boost :: bind与boost :: asio一起使用。从示例复制的boost :: bind无法正常工作

时间:2012-09-11 07:17:48

标签: c++ boost boost-asio boost-bind

有人可以告诉我为什么这不编译?我基本上是从Kholkoff(http://lists.boost.org/Archives/boost/2007/04/120339.php)的一个例子中复制了它,早在2007年,关于带有超时的套接字中的read():

void CClient::setResult(boost::optional<boost::system::error_code>*
                         a,boost::system::error_code b)
{
    *a = b;
}

我绑定()就像这样:

timer.async_wait(boost::bind(&CClient::setResult, &timer_result, _1));

gcc打印的错误对我来说是不可读的:

static assertion failed: WaitHandler type requirements not met  remote_server       line 499, external location: /usr/include/boost/asio/basic_deadline_timer.hpp   C/C++ Problem
no match for call to ‘(boost::_bi::bind_t<boost::_bi::unspecified, void (CClient::*)(boost::optional<boost::system::error_code>*, boost::system::error_code), boost::_bi::list2<boost::_bi::value<boost::optional<boost::system::error_code>*>, boost::arg<1> > >) (const boost::system::error_code&)’  remote_server       line 499, external location: /usr/include/boost/asio/basic_deadline_timer.hpp   C/C++ Problem
‘void (CClient::*)(boost::optional<boost::system::error_code>*, boost::system::error_code)’ is not a class, struct, or union type   remote_server       line 69, external location: /usr/include/boost/bind/bind.hpp    C/C++ Problem
make: *** No rule to make target `all'.             C/C++ Problem
make: *** [src/CClient.o] Error 1   remote_server           C/C++ Problem
  required from ‘class boost::_bi::bind_t<boost::_bi::unspecified, void (CClient::*)(boost::optional<boost::system::error_code>*, boost::system::error_code), boost::_bi::list2<boost::_bi::value<boost::optional<boost::system::error_code>*>, boost::arg<1> > >’  remote_server       line 15, external location: /usr/include/boost/bind/bind_template.hpp   C/C++ Problem
  required from here    CClient.cpp /remote_server/src  line 93 C/C++ Problem

1 个答案:

答案 0 :(得分:4)

怎么样

timer.async_wait(boost::bind(&CClient::setResult, this, &timer_result, _1));

setResult是成员函数,如果您在async_wait中使用CClient,或使用CClient类型的某个对象而不是this

相关问题