2013-05-02 14 views
2

有问题的代码:升压未定义的引用误差的boost ::绑定重载运算

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)' 
+0

什么是你想实现,到底? – 2013-05-02 18:57:46

+0

@EmileCormier尝试创建一个函数来确定当前对象是否属于X类型,因为它可以是{X,Y,Z}类型 – dchhetri 2013-05-02 18:59:00

+0

您是否愿意/能够使用C++ 11功能? – 2013-05-02 19:02:55

回答

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(); 
} 
+0

我看到,我的印象是代码中的第一条语句产生了一个boost函数,而不是一个布尔值。我是否阅读错误的文档http://www.boost.org/doc/libs/1_53_0/libs/bind/bind.html#operators – dchhetri 2013-05-02 19:00:29

+0

@ user814628:我不知道绑定是否支持运营商。我有一些与boost :: phoenix合作并更新了我的答案。 – 2013-05-02 19:44:09

+0

@ user814628:得到它与Boost.Pheonix和Boost.Lambda一起工作。请享用! :-) – 2013-05-02 20:13:19

相关问题