c++ - boost::函数赋值给成员函数

标签 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
        ]

最佳答案

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

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

关于c++ - boost::函数赋值给成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4613805/

相关文章:

JavaScript 参数在箭头函数中使用,但未在本地范围内的任何位置声明 - Twilio 函数

JavaScript 回调值未正确传递

javascript - 如何在循环期间访问 jquery getJson 调用($.getJson)中的索引变量?

c++ - boost::bind 和对临时变量的引用

c++ - 如果通过函数传递参数,则 sizeof 返回错误值

c++ - c++ 在声明静态成员时是否创建实例?

c++ - boost元组导致boost绑定(bind)/boost函数出现问题?

c++ - std::tr1::function::target<TFuncPtr> 和协变/逆变

c++ - 使用 auto : does not name a type, c++ 版本的 numpy arange 时出错

c++ - 删除 msvc dll 依赖以运行 qt 应用程序