我正在尝试根据其属性之一对自定义对象的 vector 进行排序,但出现以下错误:
/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
知道这个错误吗?你可以在这里找到代码:
#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));
}
最佳答案
适用于:
- 海湾合作委员会:http://rextester.com/IRO3912
- clang :http://rextester.com/FKW33178
- VC++:http://rextester.com/GNTFT92594
编译器版本:http://rextester.com/
博斯特版本:
- 1.55.0 用于 vc++
- 1.54.0 for clang
- 1.54.0 用于 gcc
关于c++ - 使用 Boost lambda 和绑定(bind)排序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23455253/