我想将计时器绑定(bind)到我的类中的回调方法。 您能帮我纠正我的代码吗?
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
最佳答案
timer.async_wait(boost::bind(&tftp_connection::callback ), this);
应该是
timer.async_wait(boost::bind(&tftp_connection::callback, this));
您需要将对象绑定(bind)到成员函数,因为如果没有它所属的对象,成员函数就无法工作。
参见boost examples for using member functions as callbacks with async_wait
关于c++ - 使用 Boost 基本截止时间计时器来调用类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22402447/