我想将一个计时器绑定到我班级的回调方法。 你能帮我纠正一下我的代码吗?
tftp_connection::tftp_connection (std::string id,
std::string file_name,
connection_type_enum connection_type,
tftp_server* server,
boost::asio::io_service& io_service)
: timer(io_service, boost::posix_time::milliseconds(5000)) {
...
//timer.async_wait(callback);
timer.async_wait(boost::bind(&tftp_connection::callback ), this);
...
}
...
void tftp_connection::callback(const boost::system::error_code& /*error*/)
{
// TIME OUT
}
错误是:
Compilation error:
/usr/local/boost_1_55_0/boost/bind/bind.hpp:69:22: Type 'void (tftp_connection::*)(const boost::system::error_code &)' cannot be used prior to '::' because it has no members
答案 0 :(得分:4)
timer.async_wait(boost::bind(&tftp_connection::callback ), this);
应该是
timer.async_wait(boost::bind(&tftp_connection::callback, this));
您需要将对象绑定到成员函数,因为成员函数在没有它所属的对象的情况下无法工作。
请参阅boost examples for using member functions as callbacks with async_wait