2011-01-06 107 views
0

我想使用boost::function並將它傳遞給一個函數作爲回調函數。我似乎在分配成員函數時遇到了一些麻煩。boost ::函數賦值給成員函數

我想傳遞給它的函數是一個靜態函數(因爲它是在另一個線程上調用的)。

boost::function<std::string (ResolverReply& reply)> call_back = std::bind1st(std::mem_fun(&ResolverCommunicator::reply_call_back), *this); 

這是ResolverCommunicator類裏面,但我的編譯器抱怨:

_Right: reference to reference is illegal 

c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(278): error C2535: 'std::binder1st<_Fn2>::result_type std::binder1st<_Fn2>::operator()(std::binder1st<_Fn2>::argument_type &) const' : member function already defined or declared 
     with 
     [ 
      _Fn2=std::mem_fun1_t<std::string,ResolverCommunicator,ResolverReply &> 
     ] 
     c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(272) : see declaration of 'std::binder1st<_Fn2>::operator`()'' 
     with 
     [ 
      _Fn2=std::mem_fun1_t<std::string,ResolverCommunicator,ResolverReply &> 
     ] 

我則只是路過call_back我被另一個線程調用靜態函數。

有誰知道什麼是錯的?

編輯:

我已經做了作爲回答說,但現在我得到這個錯誤:

error C2665: 'boost::bind' : none of the 3 overloads can convert parameter 2 from type 'ResolverCommunicator' 
     c:\Program Files\boost\boost_1_44\boost\bind\bind.hpp(1480): could be 'boost::_bi::bind_t<R,F,L> boost::bind<std::string(__thiscall ResolverCommunicator::*)(ResolverReply &),ResolverCommunicator,boost::arg<I>>(F,A1,A2)' 
     with 
     [ 
      R=boost::_bi::unspecified, 
      F=std::string (__thiscall ResolverCommunicator::*)(ResolverReply &), 
      L=boost::_bi::list2<boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B1,boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B2>, 
      I=1, 
      A1=ResolverCommunicator, 
      A2=boost::arg<1> 
     ] 
     c:\Program Files\boost\boost_1_44\boost\bind\bind_mf_cc.hpp(43): or  'boost::_bi::bind_t<R,F,L> boost::bind<std::string,ResolverCommunicator,ResolverReply&,ResolverCommunicator,boost::arg<I>>(R (__thiscall ResolverCommunicator::*)(B1),A1,A2)' 
     with 
     [ 
      R=std::string, 
      F=boost::_mfi::mf1<std::string,ResolverCommunicator,ResolverReply &>, 
      L=boost::_bi::list2<boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B1,boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B2>, 
      I=1, 
      B1=ResolverReply &, 
      A1=ResolverCommunicator, 
      A2=boost::arg<1> 
     ] 
     c:\Program Files\boost\boost_1_44\boost\bind\bind_mf_cc.hpp(54): or  'boost::_bi::bind_t<R,F,L> boost::bind<std::string,ResolverCommunicator,ResolverReply&,ResolverCommunicator,boost::arg<I>>(R (__thiscall ResolverCommunicator::*)(B1) const,A1,A2)' 
     with 
     [ 
      R=std::string, 
      F=boost::_mfi::cmf1<std::string,ResolverCommunicator,ResolverReply &>, 
      L=boost::_bi::list2<boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B1,boost::_bi::list_av_2<ResolverCommunicator,boost::arg<1>>::B2>, 
      I=1, 
      B1=ResolverReply &, 
      A1=ResolverCommunicator, 
      A2=boost::arg<1> 
     ] 
     while trying to match the argument list '(std::string (__thiscall 
ResolverCommunicator::*)(ResolverReply &), ResolverCommunicator, boost::arg<I>)' 
     with 
     [ 
      I=1 
     ] 
+0

std :: string reply_call_back(ResolverReply&reply); – 2011-01-06 10:19:33

回答

4

這是標準的粘合劑的一個已知的限制,他們不處理它採取功能其參數。你應該考慮使用boost::bind

boost::function<std::string (ResolverReply& reply)> call_back = 
    boost::bind(&ResolverCommunicator::reply_call_back, this, _1); 
+0

你可以放在這個或* this中,並且boost :: bind足夠聰明來解決這個問題。它甚至可以解析shared_ptr 作爲第二個參數。對於OP,_1是函數參數的一個佔位符(ResolverReply&) – CashCow 2011-01-06 10:31:59

+0

@CashCow你是對的,如果不包含在boost :: ref()中,使用'* this'甚至是個壞主意。 ':回答編輯 – icecrime 2011-01-06 15:10:48