boost :: asio读取处理程序类型要求未达到

时间:2019-04-18 11:52:34

标签: c++ asynchronous boost handler asio

我正在尝试将读取标头处理程序实现为成员函数,以处理传入的数据。但是在编译过程中,我被告知未满足类型要求。

我已经检查了boost文档,并且函数签名似乎还不错。我找不到区别。但是boost不接受该处理程序为有效。

.cpp文件:

void tcpclient::read_data() {
    char buffer_[1];
    boost::asio::async_read(_socket, boost::asio::buffer(buffer_, HEADER_LEN),
                            std::bind(&tcpclient::handle_read_header, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred));
}


void tcpclient::handle_read_header(const boost::system::error_code &error, std::size_t bytes_transferred) {
    if (!error) {
        logger::log_info("Read " + std::to_string(bytes_transferred) + " bytes.");
    } else {
        logger::log_error("Failed to read header");
        _socket.close();
    }
}

.h文件:

void handle_read_header(const boost::system::error_code &error, std::size_t bytes_transferred);

    void read_data();

    boost::asio::ip::tcp::socket _socket;

我希望代码能很好地接受处理程序签名,但是会提示我:

/usr/include/boost/asio/impl/read.hpp: In instantiation of ‘typename boost::asio::async_result<typename std::decay<WriteHandler>::type, void(boost::system::error_code, long unsigned int)>::return_type boost::asio::async_read(AsyncReadStream&, const MutableBufferSequence&, ReadHandler&&, typename std::enable_if<boost::asio::is_mutable_buffer_sequence<MutableBufferSequence>::value>::type*) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>; MutableBufferSequence = boost::asio::mutable_buffers_1; ReadHandler = std::_Bind<void (tcpclient::*(tcpclient*, boost::arg<1> (*)(), boost::arg<2> (*)()))(const boost::system::error_code&, long unsigned int)>; typename boost::asio::async_result<typename std::decay<WriteHandler>::type, void(boost::system::error_code, long unsigned int)>::return_type = void; typename std::enable_if<boost::asio::is_mutable_buffer_sequence<MutableBufferSequence>::value>::type = void]’:
/home/void/Documents/Development/SocketTest/SocketTest/networking/tcpclient.cpp:55:84:   required from here
/usr/include/boost/asio/impl/read.hpp:446:3: error: static assertion failed: ReadHandler type requirements not met
   BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/write.hpp:430:3: error: no match for call to ‘(std::_Bind<void (tcpclient::*(tcpclient*, boost::arg<1> (*)(), boost::arg<2> (*)()))(const boost::system::error_code&, long unsigned int)>) (const boost::system::error_code&, const long unsigned int&)’
   BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:1)

function foo(a: (s: string, n: number) => string): string { return a("hello", 10); } boost::bind一起使用

boost::asio::placeholders::..

boost::bind(&tcpclient::handle_read_header, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); std::placeholders::_1/_2

std::bind