2010-06-01 84 views
1

我想讓這段代碼正常工作,我該怎麼辦?C++問題:boost :: bind接收其他boost :: bind

在最後一行發生此錯誤。

我做錯了什麼? 我知道boost :: bind需要一個類型,但我沒有得到。幫助

class A 
{ 

public: 

    template <class Handle> 
    void bindA(Handle h) 
    { 
     h(1, 2); 
    } 
}; 

class B 
{ 

    public: 
     void bindB(int number, int number2) 
     { 
      std::cout << "1 " << number << "2 " << number2 << std::endl; 
     } 
}; 


template < class Han > struct Wrap_ 
{ 

    Wrap_(Han h) : h_(h) {} 

    template<typename Arg1, typename Arg2> void operator()(Arg1 arg1, Arg2 arg2) 
    { 
     h_(arg1, arg2); 
    } 
    Han h_; 
}; 

template< class Handler > 

    inline Wrap_<Handler> make(Handler h) 
    { 
     return Wrap_<Handler> (h); 
    } 
int main() 
{ 

    A a; 
    B b; 
    ((boost::bind)(&B::bindB, b, _1, _2))(1, 2); 
    ((boost::bind)(&A::bindA, a, make(boost::bind(&B::bindB, b, _1, _2))))(); 
/*i want compiled success and execute success this code*/ 

} 

回答

1

您遇到的問題是您嘗試綁定到模板化函數。在這種情況下,您需要指定要調用綁定的方法的模板類型。

這發生在方法A::bindA。請參閱下面的代碼片段,以便正確編譯提供的類。

順便提一下,在示例中,我使用boost::function(要綁定的姐妹庫)來指定正在使用哪種類型的函數指針。我認爲這使得它更具可讀性,並強烈建議您在繼續使用綁定時熟悉它。

#include "boost/bind.hpp" 
#include "boost/function.hpp" 

int main(int c, char** argv) 
{ 
    A a; 
    B b; 

    typedef boost::function<void(int, int)> BFunc; 
    typedef boost::function<void(BFunc)> AFunc; 
    BFunc bFunc(boost::bind(&B::bindB, b, _1, _2)); 
    AFunc aFunc(boost::bind(&A::bindA<BFunc>, a, make(bFunc))); 

    bFunc(1,2); 
}