c++ - 使用 Boost 基本截止时间计时器来调用类方法

标签 c++ boost boost-asio

我想将计时器绑定(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/

相关文章:

c++ - 使用连续内存并具有保留功能的映射和集合

sockets - 无法使用 boost asio 设置 TCP 源端口

c++ - 将输入 CREdit 控件的文本限制为 ISO 8859-1 字符

C++模板静态类代码生成

c++ - 使用 Visual Studio 构建 Google V8

c++ - 使用队列 boost asio 异步读取和写入套接字

c++ - 写入子进程的标准输入

c++ - 是否有类似 next_permutation 的函数,但用于重复排列?

c++ - 在 C++ 中发现 USB 设备并与之通信

将 asio 套接字多播 boost 到特定的以太网接口(interface)