首页 文章

使用boost :: signals2 :: signal作为处理程序时出错

提问于
浏览
0

在以下代码中:

boost::signals2::signal<void(const boost::system::error_code&)> signal;
   socket.async_connect(endpoint, boost::ref(signal));

(发现于http://liveworkspace.org/code/e04910cbcc4cfd9e42e34d1af55c393e

我收到此错误:

在boost / asio / detail / wrapped_handler.hpp中包含的文件:18:0,来自boost / asio / io_service.hpp:24,来自boost / asio / basic_io_object.hpp:20,来自boost / asio / basic_socket.hpp: 19,来自boost / asio / basic_datagram_socket.hpp:20,来自boost / asio.hpp:20,来自source.cpp:2:boost / asio / detail / bind_handler.hpp:在成员函数'void boost :: asio :: detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code :: operator()()':boost / asio / handler_invoke_hook.hpp:64:3:从'void boost :: asio :: asio_handler_invoke实例化(boost :: asio :: detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code,...)'boost / asio / detail / handler_invoke_helpers.hpp:39:3:从'void boost_asio_handler_invoke_helpers实例化:: invoke(const boost :: asio :: detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code&,boost :: reference_wrapper>&)'boost / asio / detail / bind_handler.hpp:73 :3:从'void boost :: asio :: detail :: asio_handler_invoke实例化(const boost :: asio :: detail: :binder1 <boost :: reference_wrapper>,boost :: system :: error_code&,boost :: asio :: detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code *)'boost / asio / detail /handler_invoke_helpers.hpp:39:3:从'void boost_asio_handler_invoke_helpers :: invoke(const boost :: asio :: detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code&,boost :: asio)实例化: :detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code&)'boost / asio / detail / completion_handler.hpp:63:7:从'static void boost :: asio :: detail ::实例化completion_handler <boost :: asio :: detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code :: do_complete(boost :: asio :: detail :: boost :: asio :: detail :: task_io_service * ,boost :: asio :: detail :: boost :: asio :: detail :: task_io_service_operation *,boost :: system :: error_code,unsigned int)'boost / asio / detail / completion_handler.hpp:38:17:实例化自'boost :: asio :: detail :: completion_handler <boost :: asio :: detail :: binder1 <boost :: refe rence_wrapper>,boost :: system :: error_code :: completion_handler(boost :: asio :: detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code)'boost / asio / detail / impl / task_io_service . hpp:50:3:从'void boost :: asio :: detail :: task_io_service :: post(boost :: asio :: detail :: binder1 <boost :: reference_wrapper>,boost :: system :: error_code)'实例化boost / asio / impl / io_service.hpp:80:3:从'void boost :: asio :: io_service :: post(Handler)'boost / asio / basic_socket.hpp:649:9实例化:从'void boost实例化: :asio :: basic_socket <boost :: asio :: ip :: tcp,boost :: asio :: stream_socket_service :: async_connect(boost :: asio :: basic_socket> :: boost :: asio :: ip :: basic_endpoint&, boost :: reference_wrapper>)'source.cpp:10:53:从这里实例化boost / asio / detail / bind_handler.hpp:40:5:错误:无法调用'(boost :: reference_wrapper>)(const boost) :: system :: error_code&)'

2 回答

  • 1

    Ralf的answer很接近,我认为你需要将占位符绑定到 signal 仿函数:

    #include <boost/signals2.hpp>
    #include <boost/asio.hpp>
    
    using namespace boost::asio;
    
    int 
    main() 
    {
       io_service ios;
       ip::tcp::socket socket(ios);
       const ip::tcp::endpoint endpoint(
               ip::address::from_string(""), 
               0
               );
    
       boost::signals2::signal<void(const boost::system::error_code&)> signal;
       socket.async_connect(
               endpoint, 
               boost::bind(
                   boost::ref(signal), _1
                   ) 
           );
    }
    

    例:

    samm@macmini ~$ g++ -I /opt/local/include -L/opt/local/lib -Wl,-rpath,/opt/local/lib -lboost_system signal.cc 
    samm@macmini ~$ echo $?
    0
    samm@macmini ~$
    
  • 2

    看起来你的签名是错的?您需要为error_code指定占位符

    尝试

    boost::signals2::signal<void(const boost::system::error_code&)> signal;
    socket.async_connect(endpoint, boost::bind(boost::ref(signal), _1));
    

相关问题