【问题标题】:Error when sorting with Boost lambda and bind使用 Boost lambda 排序和绑定时出错
【发布时间】:2014-05-04 10:41:57
【问题描述】:

我正在尝试根据其中一个属性对自定义对象的向量进行排序,但出现以下错误:

/usr/include/boost/lambda/detail/function_adaptors.hpp:264:15:错误:从“const int”类型的表达式中对“int&”类型的引用进行无效初始化 make: * [src/boost_lambda.o] 错误 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));
}

【问题讨论】:

  • @JoachimPileborg Boost lambda 从表达式生成函数对象。
  • @juanchopanza 这不是boost::bind,而是boost::lambda::bind

标签: c++ sorting boost lambda bind


【解决方案1】:

适用于:

编译器版本:http://rextester.com/

Bost 版本:

  • 1.55.0 用于 vc++
  • 1.54.0 用于铿锵
  • 1.54.0 用于 gcc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 2020-11-05
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多