简体   繁体   中英

boost asio doesn't recognize timer object

I'm trying to run boost example from the boost documentation timer2

this is the code:

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

void print(const boost::system::error_code& /*e*/)
{
  std::cout << "Hello, world!" << std::endl;
}

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

  boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
  t.async_wait(&print);

  io.run();

  return 0;
}

and I'm getting the following errors: error: 'io_context' is not a

member of 'boost::asio' boost::asio::io_context io; ^~~~~~~~~~ /home/mosh/Desktop/untitled1/main.cpp:62:16: note: suggested alternative: 'connect' boost::asio::io_context io; ^~~~~~~~~~ connect /home/mosh/Desktop/untitled1/main.cpp:64:16: error: 'steady_timer' is not a member of 'boost::asio'
boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5)); ^~~~~~~~~~~~ /home/mosh/Desktop/untitled1/main.cpp:64:16: note: suggested alternative: 'deadline_timer' boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5)); ^~~~~~~~~~~~ deadline_timer /home/mosh/Desktop/untitled1/main.cpp:65:3: error: 't' was not declared in this scope t.async_wait(&print); ^ /home/mosh/Desktop/untitled1/main.cpp:65:3: note: suggested alternative: 'tm' t.async_wait(&print); ^ tm /home/mosh/Desktop/untitled1/main.cpp:67:3: error: 'io' was not declared in this scope io.run(); ^~

because you have boost version 1.58 you need to change your code according to the relevant version .

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> // <- look at this header

void print(const boost::system::error_code& /*e*/)
{
  std::cout << "Hello, world!" << std::endl;
}

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

  boost::asio::deadline_timer t(io, boost::posix_time::seconds(5)); //<- this deffernt 
  t.async_wait(&print);

  io.run();

  return 0;
}

also, don't forget to link in your make/CMake file to boost_system

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM