将回调传递给boost :: asio :: async_write会带来麻烦

时间:2017-03-21 14:38:05

标签: c++ boost callback boost-asio

我在Boost :: Asio lib的帮助下编写了一个客户端 - 服务器应用程序。在我的项目中,我有 CContext 类,负责所有网络操作。这是一些代码:

class CContext {
    protected:
        ip::tcp::socket m_socket;
        streambuf m_writebuf;
        streambuf m_readbuf;
    public:
        typedef boost::function<void(const boost::system::error_code&)> callback_type;
        void async_send_message(const CMessage& msg, callback_type callback) {
            msg.to_streambuf(m_writebuf);
            async_write(m_socket, m_writebuf, callback);
        }
....
}  

尽管我当前没有在我的代码中使用 async_send_message 方法,但我得到以下编译器错误:

In file included from /usr/include/boost/asio/impl/io_service.hpp:18:0,
                 from /usr/include/boost/asio/io_service.hpp:767,
                 from /usr/include/boost/asio/basic_io_object.hpp:19,
                 from /usr/include/boost/asio/basic_socket.hpp:20,
                 from /usr/include/boost/asio/basic_datagram_socket.hpp:20,
                 from /usr/include/boost/asio.hpp:21,
                 from cclient.h:5,
                 from main.cpp:3:
/usr/include/boost/asio/impl/write.hpp: In instantiation of ‘typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_write(AsyncWriteStream&, boost::asio::basic_streambuf<Allocator>&, WriteHandler&&) [with AsyncWriteStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>; Allocator = std::allocator<char>; WriteHandler = boost::function<void(const boost::system::error_code&)>&; typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type = void]’:
../ccontext.hpp:75:46:   required from here
/usr/include/boost/asio/impl/write.hpp:746:3: error: static assertion failed: WriteHandler type requirements not met
   BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
   ^
/usr/include/boost/asio/impl/write.hpp:615:3: error: no match for call to ‘(boost::function<void(const boost::system::error_code&)>) (const boost::system::error_code&, const long unsigned int&)’
   BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
   ^
In file included from /usr/include/boost/function/detail/maybe_include.hpp:18:0,
                 from /usr/include/boost/function/detail/function_iterate.hpp:14,
                 from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52,
                 from /usr/include/boost/function.hpp:64,
                 from ../ccontext.hpp:5,
                 from cclientcontext.hpp:4,
                 from ccommand.h:14,
                 from cclient.h:13,
                 from main.cpp:3:
/usr/include/boost/function/function_template.hpp:1052:7: note: candidate is:
 class function<BOOST_FUNCTION_PARTIAL_SPEC>
       ^
/usr/include/boost/function/function_template.hpp:765:17: note: boost::function1<R, T1>::result_type boost::function1<R, T1>::operator()(T0) const [with R = void; T0 = const boost::system::error_code&; boost::function1<R, T1>::result_type = void]
     result_type operator()(BOOST_FUNCTION_PARMS) const
                 ^
/usr/include/boost/function/function_template.hpp:765:17: note:   candidate expects 1 argument, 2 provided  

如果我注释掉以下一行:

async_write(m_socket, m_writebuf, callback)  
一切似乎都没问题。是否有可能我的 callback_type boost :: asio :: async_write 不兼容?那些错误是关于什么的?它们对我来说似乎难以理解。我将不胜感激任何帮助。

0 个答案:

没有答案