【问题标题】:boost::bind together with boost::asio. boost::bind not working, copied from an exampleboost::bind 与 boost::asio 一起。 boost::bind 不起作用,从示例中复制
【发布时间】:2012-09-11 07:17:47
【问题描述】:

有人能告诉我为什么这不能编译吗?我基本上从 2007 年 Kholkoff (http://lists.boost.org/Archives/boost/2007/04/120339.php) 的示例中复制了它,关于超时套接字中的 reads():

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

【问题讨论】:

  • 请像使用缩进四个空格的代码一样格式化错误输出。这使输出变得难以阅读并且变得更糟。

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


【解决方案1】:

然后呢

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

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

【讨论】:

  • 在这种情况下,值得一提的是this必须是CClient类型。
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2012-12-08
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
相关资源
最近更新 更多