سؤال

I'd like to bind a timer to a callback method in my class. Could you please help me correcting my code ?

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
}

The error is:

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);

should be

timer.async_wait(boost::bind(&tftp_connection::callback, this));

You need to bind the object to the member function, since the member function cannot work without the object it belongs to.

See boost examples for using member functions as callbacks with async_wait

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top