boost::asio read handler type requirements not met(未满足Boost::ASIO读取处理程序类型要求)
本文介绍了未满足Boost::ASIO读取处理程序类型要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将Read Header处理程序实现为处理传入数据的成员函数。但是,在编译过程中,我被告知未满足类型要求。
我已经检查了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;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
推荐答案
使用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
:
std::bind(&tcpclient::handle_read_header, this,
std::placeholders::_1,
std::placeholders::_2));
这篇关于未满足Boost::ASIO读取处理程序类型要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:未满足Boost::ASIO读取处理程序类型要求
基础教程推荐
猜你喜欢
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 设计字符串本地化的最佳方法 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04