boost ::函数赋值给成员函数

时间:2011-01-06 10:12:56

标签: c++ callback boost-function

我想使用boost::function并将其传递给函数以充当回调函数。我似乎在为其分配成员函数时遇到了一些麻烦。

我想传递给它的函数是一个静态函数(因为它是在另一个线程上调用的)。

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
        ]

1 个答案:

答案 0 :(得分:4)

标准粘合剂的一个已知限制是它们不处理通过引用获取其参数的函数。您应该考虑使用boost::bind

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