1

我在理解尝试创建 boost::asio::deadline_timer 时遇到的编译错误时遇到了一些问题。我在 MSVC10 上使用以下代码示例得到以下错误。它是用 Boost 1.48 构建的

第一个错误看起来像是在抱怨将成员函数设置为处理程序的 async_wait 是一个引用。但是,当我更改参数时,它会给出类似的错误。

我一直在查看 Boost::Bind 和 Boost::Asio 试图找出我做错了什么。我的代码与示例类似,我使用 bind 和 deadline_timer 的方式。

很抱歉编译错误看起来有多混乱。我仍在尝试处理 Markdown。

代码示例

包括

#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/system/error_code.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

class TimerThing
{
protected:
    boost::asio::deadline_timer* statusTimer_;
    boost::thread_group worker_threads_;

    boost::asio::io_service io_service_;
    boost::shared_ptr<boost::asio::io_service::work> work_;

public:
    TimerThing() {}

    virtual ~TimerThing(){}

    void updateStatus(boost::system::error_code& ec)
    {
        if (ec == boost::asio::error::operation_aborted)
            return;

        std::cout<<"Status Update"<<std::endl;

        statusTimer_->expires_at(statusTimer_->expires_at() + boost::posix_time::seconds(1));
        statusTimer_->async_wait(boost::bind(&TimerThing::updateStatus, this, , boost::asio::placeholders::error));
    }

    void start()
    {
        statusTimer_=new boost::asio::deadline_timer(io_service_);
        boost::shared_ptr<boost::asio::io_service::work> myWork(new boost::asio::io_service::work(io_service_));
        work_=myWork;

        worker_threads_.create_thread( boost::bind( &TimerThing::threadAction, this ) );

        statusTimer_->expires_at(statusTimer_->expires_at() + boost::posix_time::seconds(1));
        statusTimer_->async_wait(boost::bind(&TimerThing::updateStatus, this, boost::asio::placeholders::error));
    }

    void threadAction()
    {
        io_service_.run();
    }

    void stop()
    {
        work_.reset();
        io_service_.stop();
        worker_threads_.join_all();

        delete statusTimer_;
    }

};



#include "TimerThing.h"

int main(int argc, const char* argv[] )
{
    TimerThing t;

    std::string input;
    std::cout<<"Press f to stop"<<std::endl;

    t.start();

    std::cin>>input;

    t.stop();

    return 0;
}

编译器错误

c:\Underware\version\include\boost/bind/bind.hpp(313): 错误 C2664: 'R boost::_mfi::mf1::operator ()(const U &,A1) const' : 无法转换参数2 从 'const boost::system::error_code' 到 'boost::system::error_code &'

     with
     [
         R=void,
         T=TimerThing,
         A1=boost::system::error_code &,
         U=TimerThing *
     ]
     Conversion loses qualifiers
     c:\Underware\version\include\boost/bind/bind_template.hpp(47) : see reference to function template instantiation 'void boost::_bi::list2<A1,A2>::operator ()<F,boost::_bi::list1<const boost::system::error_code &>>(boost::_bi::type<T>,F &,A &,int)' being compiled
     with
     [
         A1=boost::_bi::value<TimerThing *>,
         A2=boost::arg<1>,
         F=boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,
         T=void,
         A=boost::_bi::list1<const boost::system::error_code &>
     ]
     c:\Underware\version\include\boost/asio/detail/bind_handler.hpp(46) : see reference to function template instantiation 'void boost::_bi::bind_t<R,F,L>::operator ()<const Arg1>(const A1 &)' being compiled
     with
     [
         R=void,
         F=boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,
         L=boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>,
         Arg1=const boost::system::error_code,
         A1=const boost::system::error_code
     ]

     c:\Underware\version\include\boost/asio/detail/bind_handler.hpp(45) : while compiling class template member function 'void boost::asio::detail::binder1<Handler,Arg1>::operator ()(void)'
     with
     [
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>,
         Arg1=boost::system::error_code
     ]
     c:\Underware\version\include\boost/asio/detail/wait_handler.hpp(59) : see reference to class template instantiation 'boost::asio::detail::binder1<Handler,Arg1>' being compiled
     with
     [
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>,
         Arg1=boost::system::error_code
     ]

     c:\Underware\version\include\boost/asio/detail/wait_handler.hpp(45) : while compiling class template member function 'void boost::asio::detail::wait_handler<Handler>::do_complete(boost::asio::detail::io_service_impl *,boost::asio::detail::operation *,const boost::system::error_code &,size_t)'
     with
     [
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>
     ]
     c:\Underware\version\include\boost/asio/detail/deadline_timer_service.hpp(185) : see reference to class template instantiation 'boost::asio::detail::wait_handler<Handler>' being compiled
     with
     [
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>
     ]
     c:\Underware\version\include\boost/asio/deadline_timer_service.hpp(137) : see reference to function template instantiation 'void boost::asio::detail::deadline_timer_service<Time_Traits>::async_wait<WaitHandler>(boost::asio::detail::deadline_timer_service<Time_Traits>::implementation_type &,Handler)' being compiled
     with
     [
         Time_Traits=boost::asio::time_traits<boost::posix_time::ptime>,
         WaitHandler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>,
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>
     ]
     c:\Underware\version\include\boost/asio/basic_deadline_timer.hpp(502) : see reference to function template instantiation 'void boost::asio::deadline_timer_service<TimeType,TimeTraits>::async_wait<WaitHandler>(boost::asio::detail::deadline_timer_service<Time_Traits>::implementation_type &,const WaitHandler &)' being compiled
     with
     [
         TimeType=boost::posix_time::ptime,
         TimeTraits=boost::asio::time_traits<boost::posix_time::ptime>,
         WaitHandler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>,
         Time_Traits=boost::asio::time_traits<boost::posix_time::ptime>
     ]

c:\users\tharper\documents\visual studio 2010\projects\asiotimer\asiotimer\TimerThing.h(48) :参见对函数模板实例化的参考 'void boost::asio::basic_deadline_timer::async_wait>(const WaitHandler &) ' 正在编译

     with
     [
         Time=boost::posix_time::ptime,
         R=void,
         F=boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,
         L=boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>,

WaitHandler=boost::_bi::bind_t,boost::_bi::list2,boost::arg<1>>>
] TimerThing.cpp

c:\Underware\version\include\boost/bind/bind.hpp(313): 错误 C2664: 'R boost::_mfi::mf1::operator ()(const U &,A1) const' : 无法转换参数2 从 'const boost::system::error_code' 到 'boost::system::error_code &'

     with
     [
         R=void,
         T=TimerThing,
         A1=boost::system::error_code &,
         U=TimerThing *
     ]
     Conversion loses qualifiers

c:\Underware\version\include\boost/bind/bind_template.hpp(47) : 参见函数模板实例化 'void boost::_bi::list2::operator ()>(boost::_bi::type, F &,A &,int)' 正在编译

     with
     [
         A1=boost::_bi::value<TimerThing *>,
         A2=boost::arg<1>,
         F=boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,
         T=void,
         A=boost::_bi::list1<const boost::system::error_code &>
     ]
     c:\Underware\version\include\boost/asio/detail/bind_handler.hpp(46) : see reference to function template instantiation 'void boost::_bi::bind_t<R,F,L>::operator ()<const Arg1>(const A1 &)' being compiled
     with
     [
         R=void,
         F=boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,
         L=boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<>>,
         Arg1=const boost::system::error_code,
         A1=const boost::system::error_code
     ]
     c:\Underware\version\include\boost/asio/detail/bind_handler.hpp(45) : while compiling class template member function 'void boost::asio::detail::binder1<Handler,Arg1>::operator ()(void)'
     with
     [
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>,
         Arg1=boost::system::error_code
     ]
     c:\Underware\version\include\boost/asio/detail/wait_handler.hpp(59) : see reference to class template instantiation 'boost::asio::detail::binder1<Handler,Arg1>' being compiled
     with
     [
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>,
         Arg1=boost::system::error_code
     ]
     c:\Underware\version\include\boost/asio/detail/wait_handler.hpp(45) : while compiling class template member function 'void boost::asio::detail::wait_handler<Handler>::do_complete(boost::asio::detail::io_service_impl *,boost::asio::detail::operation *,const boost::system::error_code &,size_t)'
     with
     [
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>
     ]
     c:\Underware\version\include\boost/asio/detail/deadline_timer_service.hpp(185) : see reference to class template instantiation 'boost::asio::detail::wait_handler<Handler>' being compiled
     with
     [
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>
     ]
     c:\Underware\version\include\boost/asio/deadline_timer_service.hpp(137) : see reference to function template instantiation 'void boost::asio::detail::deadline_timer_service<Time_Traits>::async_wait<WaitHandler>(boost::asio::detail::deadline_timer_service<Time_Traits>::implementation_type &,Handler)' being compiled
     with
     [
         Time_Traits=boost::asio::time_traits<boost::posix_time::ptime>,
         WaitHandler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>,
         Handler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>
     ]
     c:\Underware\version\include\boost/asio/basic_deadline_timer.hpp(502) : see reference to function template instantiation 'void boost::asio::deadline_timer_service<TimeType,TimeTraits>::async_wait<WaitHandler>(boost::asio::detail::deadline_timer_service<Time_Traits>::implementation_type &,const WaitHandler &)' being compiled
     with
     [
         TimeType=boost::posix_time::ptime,
         TimeTraits=boost::asio::time_traits<boost::posix_time::ptime>,
         WaitHandler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>,
         Time_Traits=boost::asio::time_traits<boost::posix_time::ptime>
     ]
     c:\users\tharper\documents\visual studio 2010\projects\asiotimer\asiotimer\TimerThing.h(48) : see reference to function template instantiation 'void boost::asio::basic_deadline_timer<Time>::async_wait<boost::_bi::bind_t<R,F,L>>(const WaitHandler &)' being compiled
     with
     [
         Time=boost::posix_time::ptime,
         R=void,
         F=boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,
         L=boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>,
         WaitHandler=boost::_bi::bind_t<void,boost::_mfi::mf1<void,TimerThing,boost::system::error_code &>,boost::_bi::list2<boost::_bi::value<TimerThing *>,boost::arg<1>>>
     ]
4

2 回答 2

4

void updateStatus(boost::system::error_code& ec)应该是void updateStatus(const boost::system::error_code& ec)

你有额外的逗号boost::bind(&TimerThing::updateStatus, this, , boost::asio::placeholders::error));

于 2012-11-21T20:25:20.600 回答
2

你的绑定start是正确的,一个updateStatus是不正确的:

void updateStatus(const boost::system::error_code& ec)
{
    if (ec == boost::asio::error::operation_aborted)
        return;

    std::cout<<"Status Update"<<std::endl;

    statusTimer_->expires_at(
      statusTimer_->expires_at() + boost::posix_time::seconds(1));
    statusTimer_->async_wait(
      boost::bind(&TimerThing::updateStatus, this, 
                  boost::asio::placeholders::error));
}

您还需要使用占位符。

一般提示:更喜欢发布可编译的示例而不是完整的编译器。在您的情况下,删除不可移植的 VC 垃圾(tmain, stdafx)并添加所有包含。

于 2012-11-18T20:46:43.217 回答