Search code examples
c++boostboost-asioasiodeadline-timer

Errors trying to use boost::asio::deadline_timer in class


Boost v1.54

I had originally written my tests for this inside a single .cpp file which worked fine, however when trying to merge it into my source code with my class, I am getting persisting errors of:

error: no matching function for call to ‘boost::posix_time::seconds::seconds()’
 Database::Database()

error: no matching function for call to ‘boost::asio::basic_deadline_timer<boost::posix_time::ptime>::basic_deadline_timer()’
 Database::Database()

error: no match for call to ‘(boost::posix_time::seconds) (int)’
     interval(1);  // 1 second

I did some reading and saw I had to initialise these in my constructor, and attempted that, however that did not solve my problem.

a.cpp - Database Constructor

Database::Database()
{
    interval(1);
    timer(io_service,interval);    
}

a.h

class Database
{
public:
    boost::posix_time::seconds interval;
    boost::asio::io_service io_service;
    boost::asio::deadline_timer timer;    
};

EDIT I have solved my problem for now by initialising the constructor as a list with:

Database::Database()
    : interval(1), timer(io_service, interval) {};

Solution

  • I have solved my problem for now by initialising the constructor as a list with:

    Database::Database()
        : interval(1), timer(io_service, interval) {};