【问题标题】:Boost undefined reference error with boost::bind overloaded operators使用 boost::bind 重载运算符提升未定义的引用错误
【发布时间】:2013-05-02 18:46:11
【问题描述】:

有问题的代码:

boost::function<bool()> isSpecialWeapon = boost::bind(&WeaponBase::GetType,this) == WeaponType::SPECIAL_WEAPON;

我得到的错误是这样的:

 undefined reference to `boost::_bi::bind_t<bool, boost::_bi::equal, 
 boost::_bi::list2<boost::_bi::bind_t<WeaponType::Guns, 
 boost::_mfi::cmf0<WeaponType::Guns, WeaponBase>,  
 boost::_bi::list1<boost::_bi::value<WeaponBase*> > >, 
 boost::_bi::add_value<WeaponType::Guns>::type> > boost::_bi::operator==
 <WeaponType::Guns, boost::_mfi::cmf0<WeaponType::Guns, WeaponBase>, 
 boost::_bi::list1<boost::_bi::value<WeaponBase*> >, WeaponType::Guns>
 (boost::_bi::bind_t<WeaponType::Guns, boost::_mfi::cmf0<WeaponType::Guns, WeaponBase>, 
 boost::_bi::list1<boost::_bi::value<WeaponBase*> > > const&, WeaponType::Guns)'

【问题讨论】:

  • 你到底想完成什么?
  • @EmileCormier 尝试创建一个函数来确定当前对象是否属于 X 类型,因为它可以是 {X,Y,Z} 类型
  • 你愿意/能够使用 C++11 的特性吗?
  • @EmileCormier 抱歉,我正在使用的系统不支持 C++11,否则我会使用 lamdas。你能告诉我我是否读错了文档boost.org/doc/libs/1_53_0/libs/bind/bind.html#operators
  • 你用的是什么编译器?从文档看来,它应该可以工作。因为与值比较的绑定函子应该返回一个新的绑定函子来进行比较。

标签: c++ templates boost linker boost-bind


【解决方案1】:

如果您无法让 boost::bind 按您的意愿工作,您可以尝试 Boost.Pheonix 或 Boost.Lamda 作为解决方法。

尝试使用boost::pheonix::bind(来自Boost.Pheonix)而不是boost::bind

#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/bind/bind_member_function.hpp>
#include <boost/function.hpp>
#include <iostream>

enum WeaponType {melee, ranged, special};

class Sword
{
public:
    WeaponType GetType() const {return melee;}

    void test()
    {
        namespace bp = boost::phoenix;
        boost::function<bool()> isSpecialWeapon =
            bp::bind(&Sword::GetType, this) == special;
        std::cout << "isSpecialWeapon() = " << isSpecialWeapon() << "\n";
    }

};

int main()
{
    Sword sword;
    sword.test();
}

或者,您也可以使用boost::lambda::bind(来自Boost.Lambda):

#include <boost/function.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <iostream>

enum WeaponType {melee, ranged, special};

class Sword
{
public:
    WeaponType GetType() const {return melee;}

    void test()
    {
        boost::function<bool()> isSpecialWeapon =
            boost::lambda::bind(&Sword::GetType, this) == special;
        std::cout << "isSpecialWeapon() = " << isSpecialWeapon() << "\n";
    }

};

int main()
{
    Sword sword;
    sword.test();
}

【讨论】:

  • 我明白了,我的印象是你的代码中的第一条语句产生了一个 boost 函数而不是一个布尔值。我是不是读错了文档boost.org/doc/libs/1_53_0/libs/bind/bind.html#operators
  • @user814628:我不知道 bind 支持运算符。我有一些与 boost::phoenix 合作的东西并更新了我的答案。
  • @user814628:可以同时使用 Boost.Pheonix 和 Boost.Lambda。享受! :-)
  • 太棒了,这很奇怪,我必须确保我的代码适用于两个不同的平台,原始代码在一个平台上编译,但在另一个平台上因给定错误而失败。我希望我的代码是统一的,所以我没有为不同的平台设置 ifdef,而是创建了一个本地函数,而不是使用内联绑定,这似乎在两个平台上都有效。感谢您的 phoenix 和 lamda 实施。我将不得不研究这些命名空间,看看它们的绑定有什么不同或更好。
猜你喜欢
  • 1970-01-01
  • 2018-09-17
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2015-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多