Question

I am trying to sort a vector of custom objects based on one of its attributes, and I am getting the following error:

/usr/include/boost/lambda/detail/function_adaptors.hpp:264:15: error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ make: * [src/boost_lambda.o] Error 1

Any idea about this error? You can find here the code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

struct Parent{
    int getAge(){
    return age;
}
int age;
std::string name;
};

int main()
{
    std::vector<Parent> aListParents;

    Parent aParent1;
    aParent1.age=1;
    aParent1.name="parent1";
    Parent aParent2;
    aParent2.age=2;
    aParent2.name="parent2";

    aListParents.push_back(aParent1);
    aListParents.push_back(aParent2);

    std::sort(aListParents.begin(), aListParents.end(),
        bind(&Parent::age, boost::lambda::_1)  < bind(&Parent::age, boost::lambda::_2));
}
Was it helpful?

Solution

Works fine for:

Compilers versions: http://rextester.com/

Bost versions:

  • 1.55.0 for vc++
  • 1.54.0 for clang
  • 1.54.0 for gcc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top