提升计时器绑定

时间:2009-08-14 09:10:57

标签: boost timer

我正在尝试使用boost库设置计时器。

基本上,这就是我写的:

class ContactList {
public:
    ContactList();
    virtual ~ContactList();
    int Start();
    int readFromFile();
    int dumpList();
    void checkContacts(const boost::system::error_code& error);
    void initializeTimer();

private:
    std::list<Contact*> CList;
    boost::asio::deadline_timer dt;
    boost::asio::io_service ios;
};

int ContactList::Start(){
...
this->initializeTimer();
}

void ContactList::checkContacts(const boost::system::error_code& error){
    std::cout<<"__Here we go!"<<std::endl;
    ios.reset();
    initializeTimer();
}

void ContactList::initializeTimer(){
    dt.expires_from_now(boost::posix_time::seconds(4));
    dt.async_wait(boost::bind( &ContactList::checkContacts, this, boost::asio::placeholders::error ) );
    ios.run();
}

并且编译器抱怨很多,说:

   g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"kad/ContactList.d" -MT"kad/ContactList.d" -o"kad/ContactList.o" "../kad/ContactList.cpp"
../kad/ContactList.cpp: In constructor ‘kad::ContactList::ContactList()’:
../kad/ContactList.cpp:12: error: no matching function for call to ‘boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> > >::basic_deadline_timer()’
/usr/include/boost/asio/basic_deadline_timer.hpp:177: note: candidates are: boost::asio::basic_deadline_timer<Time, TimeTraits, TimerService>::basic_deadline_timer(boost::asio::io_service&, const typename TimeTraits::duration_type&) [with Time = boost::posix_time::ptime, TimeTraits = boost::asio::time_traits<boost::posix_time::ptime>, TimerService = boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >]
/usr/include/boost/asio/basic_deadline_timer.hpp:158: note:                 boost::asio::basic_deadline_timer<Time, TimeTraits, TimerService>::basic_deadline_timer(boost::asio::io_service&, const typename TimeTraits::time_type&) [with Time = boost::posix_time::ptime, TimeTraits = boost::asio::time_traits<boost::posix_time::ptime>, TimerService = boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >]
/usr/include/boost/asio/basic_deadline_timer.hpp:142: note:                 boost::asio::basic_deadline_timer<Time, TimeTraits, TimerService>::basic_deadline_timer(boost::asio::io_service&) [with Time = boost::posix_time::ptime, TimeTraits = boost::asio::time_traits<boost::posix_time::ptime>, TimerService = boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >]
/usr/include/boost/asio/basic_deadline_timer.hpp:122: note:                 boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> > >::basic_deadline_timer(const boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> > >&)
/usr/include/boost/mem_fn.hpp: In member function ‘R& boost::_mfi::dm<R, T>::operator()(T*) const [with R = void ()(const boost::system::error_code&), T = kad::ContactList]’:
/usr/include/boost/bind.hpp:222:   instantiated from ‘R boost::_bi::list1<A1>::operator()(boost::_bi::type<R>, F&, A&, long int) [with R = void (&)(const boost::system::error_code&), F = boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, A = boost::_bi::list1<boost::system::error_code&>, A1 = boost::_bi::value<kad::ContactList*>]’
/usr/include/boost/bind/bind_template.hpp:32:   instantiated from ‘typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = boost::system::error_code, R = void (&)(const boost::system::error_code&), F = boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, L = boost::_bi::list1<boost::_bi::value<kad::ContactList*> >]’
/usr/include/boost/asio/detail/bind_handler.hpp:39:   instantiated from ‘void boost::asio::detail::binder1<Handler, Arg1>::operator()() [with Handler = boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, Arg1 = boost::system::error_code]’
/usr/include/boost/asio/handler_invoke_hook.hpp:63:   instantiated from ‘void boost::asio::asio_handler_invoke(Function, ...) [with Function = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>]’
/usr/include/boost/asio/detail/handler_invoke_helpers.hpp:39:   instantiated from ‘void boost_asio_handler_invoke_helpers::invoke(const Function&, Context*) [with Function = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>, Context = boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >]’
/usr/include/boost/asio/detail/bind_handler.hpp:72:   instantiated from ‘void boost::asio::detail::asio_handler_invoke(const Function&, boost::asio::detail::binder1<Handler, Arg1>*) [with Function = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>, Handler = boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, Arg1 = boost::system::error_code]’
/usr/include/boost/asio/detail/handler_invoke_helpers.hpp:39:   instantiated from ‘void boost_asio_handler_invoke_helpers::invoke(const Function&, Context*) [with Function = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>, Context = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>]’
/usr/include/boost/asio/detail/handler_queue.hpp:192:   instantiated from ‘static void boost::asio::detail::handler_queue::handler_wrapper<Handler>::do_call(boost::asio::detail::handler_queue::handler*) [with Handler = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>]’
/usr/include/boost/asio/detail/handler_queue.hpp:172:   instantiated from ‘boost::asio::detail::handler_queue::handler_wrapper<Handler>::handler_wrapper(Handler) [with Handler = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>]’
/usr/include/boost/asio/detail/handler_alloc_helpers.hpp:138:   instantiated from ‘boost::asio::detail::handler_ptr<Alloc_Traits>::handler_ptr(boost::asio::detail::raw_handler_ptr<Alloc_Traits>&, Arg1&) [with Arg1 = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>, Alloc_Traits = boost::asio::detail::handler_alloc_traits<boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>, boost::asio::detail::handler_queue::handler_wrapper<boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code> > >]’
/usr/include/boost/asio/detail/handler_queue.hpp:117:   instantiated from ‘static boost::asio::detail::handler_queue::handler* boost::asio::detail::handler_queue::wrap(Handler) [with Handler = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>]’
/usr/include/boost/asio/detail/task_io_service.hpp:190:   instantiated from ‘void boost::asio::detail::task_io_service<Task>::post(Handler) [with Handler = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>, Task = boost::asio::detail::epoll_reactor<false>]’
/usr/include/boost/asio/impl/io_service.ipp:126:   instantiated from ‘void boost::asio::io_service::post(Handler) [with CompletionHandler = boost::asio::detail::binder1<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, boost::system::error_code>]’
/usr/include/boost/asio/detail/deadline_timer_service.hpp:172:   instantiated from ‘void boost::asio::detail::deadline_timer_service<Time_Traits, Timer_Scheduler>::wait_handler<Handler>::operator()(const boost::system::error_code&) [with Handler = boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, Time_Traits = boost::asio::time_traits<boost::posix_time::ptime>, Timer_Scheduler = boost::asio::detail::epoll_reactor<false>]’
/usr/include/boost/asio/detail/timer_queue.hpp:299:   instantiated from ‘static void boost::asio::detail::timer_queue<Time_Traits>::timer<Handler>::complete_handler(boost::asio::detail::timer_queue<Time_Traits>::timer_base*, const boost::system::error_code&) [with Handler = boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::detail::epoll_reactor<false> >::wait_handler<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > > >, Time_Traits = boost::asio::time_traits<boost::posix_time::ptime>]’
/usr/include/boost/asio/detail/timer_queue.hpp:276:   instantiated from ‘boost::asio::detail::timer_queue<Time_Traits>::timer<Handler>::timer(const typename Time_Traits::time_type&, Handler, void*) [with Handler = boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::detail::epoll_reactor<false> >::wait_handler<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > > >, Time_Traits = boost::asio::time_traits<boost::posix_time::ptime>]’
/usr/include/boost/asio/detail/timer_queue.hpp:71:   instantiated from ‘bool boost::asio::detail::timer_queue<Time_Traits>::enqueue_timer(const typename Time_Traits::time_type&, Handler, void*) [with Handler = boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::detail::epoll_reactor<false> >::wait_handler<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > > >, Time_Traits = boost::asio::time_traits<boost::posix_time::ptime>]’
/usr/include/boost/asio/detail/epoll_reactor.hpp:414:   instantiated from ‘void boost::asio::detail::epoll_reactor<Own_Thread>::schedule_timer(boost::asio::detail::timer_queue<Time_Traits>&, const typename Time_Traits::time_type&, Handler, void*) [with Time_Traits = boost::asio::time_traits<boost::posix_time::ptime>, Handler = boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::detail::epoll_reactor<false> >::wait_handler<boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > > >, bool Own_Thread = false]’
/usr/include/boost/asio/detail/deadline_timer_service.hpp:185:   instantiated from ‘void boost::asio::detail::deadline_timer_service<Time_Traits, Timer_Scheduler>::async_wait(boost::asio::detail::deadline_timer_service<Time_Traits, Timer_Scheduler>::implementation_type&, Handler) [with Handler = boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, Time_Traits = boost::asio::time_traits<boost::posix_time::ptime>, Timer_Scheduler = boost::asio::detail::epoll_reactor<false>]’
/usr/include/boost/asio/deadline_timer_service.hpp:157:   instantiated from ‘void boost::asio::deadline_timer_service<TimeType, TimeTraits>::async_wait(typename boost::asio::detail::deadline_timer_service<TimeTraits, boost::asio::detail::epoll_reactor<false> >::implementation_type&, WaitHandler) [with WaitHandler = boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, TimeType = boost::posix_time::ptime, TimeTraits = boost::asio::time_traits<boost::posix_time::ptime>]’
/usr/include/boost/asio/basic_deadline_timer.hpp:374:   instantiated from ‘void boost::asio::basic_deadline_timer<Time, TimeTraits, TimerService>::async_wait(WaitHandler) [with WaitHandler = boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void ()(const boost::system::error_code&), kad::ContactList>, boost::_bi::list1<boost::_bi::value<kad::ContactList*> > >, Time = boost::posix_time::ptime, TimeTraits = boost::asio::time_traits<boost::posix_time::ptime>, TimerService = boost::asio::deadline_timer_service<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >]’
../kad/ContactList.cpp:96:   instantiated from here
/usr/include/boost/mem_fn.hpp:342: error: invalid use of non-static member function
make: *** [kad/ContactList.o] Error 1

我真的很感激任何帮助或建议。 提前谢谢

1 个答案:

答案 0 :(得分:4)

我怀疑编译器正在尝试(并且失败)告诉你问题在于你的构造函数。你没有包含它的代码,但我猜你没有初始化deadline_timer成员dt。也许你做这样的事情:

ContactList::ContactList()
{
}

问题是,deadline_timer类没有定义默认构造函数 - 您必须选择defined constructors之一。似乎有三种可供选择:

basic_deadline_timer(
    boost::asio::io_service & io_service);

basic_deadline_timer(
    boost::asio::io_service & io_service,
    const time_type & expiry_time);

basic_deadline_timer(
    boost::asio::io_service & io_service,
    const duration_type & expiry_time);

显然模板错误很难阅读,并且包含大量无用的信息 - 解析它们是一种艺术形式。您可能希望研究安装stlfilt - 它是一组perl脚本,可以在某种程度上简化错误消息。