1

我想使用 aboost::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
        ]
4

1 回答 1

5

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

boost::function<std::string (ResolverReply& reply)> call_back =
    boost::bind(&ResolverCommunicator::reply_call_back, this, _1);
于 2011-01-06T10:15:35.223 回答