2011-01-06 109 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