This function is returning the error "matrixOpsLibrary.h:11: error: invalid initialization of reference of type ‘const std::Vec&’ from expression of type ‘const double’"

#include <vector>
#include <cmath>

using namespace std;

typedef std::vector<double> Vec; //Vector

Vec floor( const Vec& y ){
	unsigned n = y.size();
	Vec bottom(n);
	for( unsigned e=0; e<n; ++e ) bottom[e] = floor(y[e]);
return bottom;
}

Recommended Answers

All 3 Replies

There is nothing intrinsically wrong with your code. i.e. in a stand alone test env. it works.

The error you are getting comes from your library matrixOptLibrary.h. There are any number of reasons that this code is going to break it. (a) the using namespace std; ,
and more likely (b) that your typedef is in a include file and it is not in a namespace, and it is above an #include "matrixOptLibrary.h".

If you can post a small complete test code that produces the error, we will have another look. Otherwise pay particular attention to your .h files, particularly if you also have #include lines in them.

Member Avatar for danb737

Or you could use STL to do this for you applying the floor function on your vector and returning the result in another vector.

#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>

int main()
{
    typedef std::vector<double> Vec;
    Vec first;
    //Fill this vector with double values

    Vec second(first.size());
    std::pointer_to_unary_function <double, double> floorObject(std::floor);
    std::transform(first.begin(), first.end(), second.begin(), floorObject);

    return 0;
}
commented: Nice, didn't know about pointer_to_unary_function +13

My actual code was not using the std namespace. Therefore I needed "std::floor" instead of just "floor". Thanks StuXYZ and akhena for your advice. :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.