首页 文章

尝试为boost :::: asio :: deadline_timer设置成员函数时出现Boost :: bind编译错误

提问于
浏览
1

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

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

我做错了 . 我的代码类似于this示例,我使用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&'


[
R =无效,
T = TimerThing,
A1 = boost :: system :: error_code&,
U = TimerThing *
]
转换失去限定符
c:\ Underware \ version \ include \ boost / bind / bind_template.hpp(47):参见函数模板实例化'void boost :: _ bi :: list2 <A1,A2> :: operator()<F,boost: :_bi :: list1 <const boost :: system :: error_code&>>(boost :: _ bi :: type <T>,F&,A&,int)'正在编译

[
A1 = boost :: _ bi :: value <TimerThing *>,
A2 =升压:: ARG <1>,
F = boost :: _ mfi :: mf1 <void,TimerThing,boost :: system :: error_code&>,
T =无效,
A = boost :: _ bi :: list1 <const boost :: system :: error_code&>
]
c:\ Underware \ version \ include \ boost / asio / detail / bind_handler.hpp(46):参见函数模板实例化'void boost :: _ bi :: bind_t <R,F,L> :: operator()< const Arg1>(const A1&)'正在编译中

[
R =无效,
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):同时编译类模板成员函数'void boost :: asio :: detail :: binder1 <Handler,Arg1> :: operator() (无效)”

[
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 ::系统:: ERROR_CODE
]
c:\ Underware \ version \ include \ boost / asio / detail / wait_handler.hpp(59):参见类模板实例化'boost :: asio :: detail :: binder1 <Handler,Arg1>'正在编译

[
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 ::系统:: ERROR_CODE
]

c:\ Underware \ version \ include \ boost / asio / detail / wait_handler.hpp(45):同时编译类模板成员函数'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)'

[
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):参见类模板实例化'boost :: asio :: detail :: wait_handler <Handler>'的编译

[
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):参见函数模板实例化'void boost :: asio :: detail :: deadline_timer_service <Time_Traits> :: async_wait <WaitHandler>(提升: :asio :: detail :: deadline_timer_service <Time_Traits> :: implementation_type&,Handler)'正在编译

[
Time_Traits =升压:: ASIO :: time_traits <提高::了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):参见函数模板实例化的参考'voidboost :: asio :: deadline_timer_service <TimeType,TimeTraits> :: async_wait <WaitHandler>(boost :: asio :: detail :: deadline_timer_service <Time_Traits> :: implementation_type&,const WaitHandler&)'正在编译中

[
TimeType =提振::了posix_time ::的ptime,
TimeTraits =升压:: ASIO :: time_traits <提高::了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 =升压:: ASIO :: time_traits <提高::了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&) '正在编译


[
时间=提振::了posix_time ::的ptime,
R =无效,
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&'


[
R =无效,
T = TimerThing,
A1 = boost :: system :: error_code&,
U = TimerThing *
]
转换失去限定符

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


[
A1 = boost :: _ bi :: value <TimerThing *>,
A2 =升压:: ARG <1>,
F = boost :: _ mfi :: mf1 <void,TimerThing,boost :: system :: error_code&>,
T =无效,
A = boost :: _ bi :: list1 <const boost :: system :: error_code&>
]
c:\ Underware \ version \ include \ boost / asio / detail / bind_handler.hpp(46):参见函数模板实例化'void boost :: _ bi :: bind_t <R,F,L> :: operator()< const Arg1>(const A1&)'正在编译中

[
R =无效,
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):同时编译类模板成员函数'void boost :: asio :: detail :: binder1 <Handler,Arg1> :: operator() (无效)”

[
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 ::系统:: ERROR_CODE
]
c:\ Underware \ version \ include \ boost / asio / detail / wait_handler.hpp(59):参见类模板实例化'boost :: asio :: detail :: binder1 <Handler,Arg1>'正在编译

[
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 ::系统:: ERROR_CODE
]
c:\ Underware \ version \ include \ boost / asio / detail / wait_handler.hpp(45):同时编译类模板成员函数'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)'

[
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):参见类模板实例化'boost :: asio :: detail :: wait_handler <Handler>'的编译

[
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):参见函数模板实例化'void boost :: asio :: detail :: deadline_timer_service <Time_Traits> :: async_wait <WaitHandler>(提升: :asio :: detail :: deadline_timer_service <Time_Traits> :: implementation_type&,Handler)'正在编译

[
Time_Traits =升压:: ASIO :: time_traits <提高::了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
>,升压:: ARG <1 >>>
]
c:\ Underware \ version \ include \ boost / asio / basic_deadline_timer.hpp(502):参见函数模板实例化'void boost :: asio :: deadline_timer_service <TimeType,TimeTraits> :: async_wait <WaitHandler>(boost :: asio :: detail :: deadline_timer_service <Time_Traits> :: implementation_type&,const WaitHandler&)'正在编译

[
TimeType =提振::了posix_time ::的ptime,
TimeTraits =升压:: ASIO :: time_traits <提高::了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 =升压:: ASIO :: time_traits <提高::了posix_time ::的ptime>
]
c:\ users \ tharper \ documents \ visual studio 2010 \ projects \ asiotimer \ asiotimer \ TimerThing.h(48):参见函数模板实例化'void boost :: asio :: basic_deadline_timer <Time> :: async_wait <boost: :_bi :: bind_t <R,F,L >>(const WaitHandler&)'正在编译

[
时间=提振::了posix_time ::的ptime,
R =无效,
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 >>>
]

2 回答

  • 2

    void updateStatus(boost::system::error_code& ec) 应为 void updateStatus(const boost::system::error_code& ec) .

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

  • 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));
    }
    

    您还需要使用占位符 .

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

相关问题