Search code examples
c++boost-asio

Boost no matching member function for call to 'async_wait'


I am learning boost and trying to run example code: https://www.boost.org/doc/libs/master/doc/html/boost_asio/tutorial/tuttimer3/src.html

Boost version is 1.83.0

I have an error:

main.cpp:14:8: error: no matching member function for call to 'async_wait'
    t->async_wait(std::bind(print,
    ~~~^~~~~~~~~~

My code

#include <functional>
#include <iostream>
#include <boost/asio.hpp>

void print(const boost::system::error_code& /*e*/,
    boost::asio::steady_timer* t, int* count)
{
  if (*count < 5)
  {
    std::cout << *count << std::endl;
    ++(*count);

    t->expires_at(t->expiry() + boost::asio::chrono::seconds(1));
    t->async_wait(std::bind(print,
          boost::asio::placeholders::error, t, count));
  }
}

int main()
{
  boost::asio::io_context io;

  int count = 0;
  boost::asio::steady_timer t(io, boost::asio::chrono::seconds(1));
  t.async_wait(std::bind(print,
        boost::asio::placeholders::error, &t, &count));

  io.run();

  std::cout << "Final count is " << count << std::endl;

  return 0;
}

Solution

  • You are using Boost version 1.83.0, so you have to follow the correct version of the documentation: https://www.boost.org/doc/libs/1_83_0/doc/html/boost_asio/tutorial/tuttimer3/src.html (note the 1_83_0 part, not master).

    In particular, note that boost::bind is used instead of std::bind, and you have to #include <boost/bind/bind.hpp>.

    (By the way, the code in your question has includes missing whatsoever, so it's not a Minimal Reproducible Example, making it harder to diagnose.)