2013-05-02 13 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

相關問題