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

时间:2011-02-28 22:32:07

标签: c++ boost boost-asio

在以下代码中:

   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&lt;       boost :: reference_wrapper&gt;,       提高::系统:: ERROR_CODE   ::运算符()()“:   boost / asio / handler_invoke_hook.hpp:64:3:从'void boost :: asio :: asio_handler_invoke实例化(       提高:: ASIO ::详细:: binder1&LT;         boost :: reference_wrapper&gt;,         提高::系统:: ERROR_CODE   ,...     )”   boost / asio / detail / handler_invoke_helpers.hpp:39:3:从'void实例化     boost_asio_handler_invoke_helpers ::调用(       const boost :: asio :: detail :: binder1&lt;         boost :: reference_wrapper&gt;,         提高::系统:: ERROR_CODE   &amp;,boost :: reference_wrapper&gt; &安培;     )”   boost / asio / detail / bind_handler.hpp:73:3:从'void boost :: asio :: detail实例化     :: asio_handler_invoke(       const boost :: asio :: detail :: binder1&lt;         boost :: reference_wrapper&gt;,         提高::系统:: ERROR_CODE   &安培;,       提高:: ASIO ::详细:: binder1&LT;         boost :: reference_wrapper&gt;,         提高::系统:: ERROR_CODE   *     )”   boost / asio / detail / handler_invoke_helpers.hpp:39:3:从'void实例化     boost_asio_handler_invoke_helpers ::调用(       const boost :: asio :: detail :: binder1&lt;         boost :: reference_wrapper&gt;,         提高::系统:: ERROR_CODE   &安培;,       提高:: ASIO ::详细:: binder1&LT;         boost :: reference_wrapper&gt;,         提高::系统:: ERROR_CODE   &安培;     )”   boost / asio / detail / completion_handler.hpp:63:7:从'static void boost :: asio :: detail实例化     :: completion_handler&LT;       提高:: ASIO ::详细:: binder1&LT;         boost :: reference_wrapper&gt;,         升压::系统:: 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&LT;       提高:: ASIO ::详细:: binder1&LT;         boost :: reference_wrapper&gt;,         升压::系统:: ERROR_CODE

     

:: completion_handler(       提高:: ASIO ::详细:: binder1&LT;         boost :: reference_wrapper&gt;,         升压::系统:: ERROR_CODE

     

)”   boost / asio / detail / impl / task_io_service.hpp:50:3:从'void boost :: asio :: detail实例化     :: task_io_service ::后(       提高:: ASIO ::详细:: binder1&LT;         boost :: reference_wrapper&gt;,         升压::系统:: 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&lt;实例化       boost :: asio :: ip :: tcp,boost :: asio :: stream_socket_service   :: async_connect(       boost :: asio :: basic_socket&gt; :: boost       :: asio :: ip :: basic_endpoint&amp ;,       升压::的reference_wrapper&GT;     )”   source.cpp:10:53:从这里实例化   boost / asio / detail / bind_handler.hpp:40:5:错误:调用'不匹配'(       升压::的reference_wrapper&GT;     )(const boost :: system :: error_code&amp;)'

2 个答案:

答案 0 :(得分:2)

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

尝试

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

答案 1 :(得分: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 ~$