boost::asio::async_read绑定编译错误
boost::asio::async_read bind compilation error
我不明白为什么会出现这个错误:
/usr/local/include/boost/asio/impl/read.hpp: In member function ‘void boost::asio::detail::read_op<AsyncReadStream, boost::asio::mutable_buffers_1, CompletionCondition, ReadHandler>::operator()(const boost::system::error_code&, size_t, int) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, CompletionCondition = boost::asio::detail::transfer_at_least_t, ReadHandler = boost::function<void ()(long unsigned int)>]’:
/usr/local/include/boost/asio/impl/read.hpp:263: instantiated from ‘void boost::asio::async_read(AsyncReadStream&, const MutableBufferSequence&, CompletionCondition, ReadHandler) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, MutableBufferSequence = boost::asio::mutable_buffers_1, CompletionCondition = boost::asio::detail::transfer_at_least_t, ReadHandler = boost::function<void ()(long unsigned int)>]’
src/communicator/protocol/Command.cc:34: instantiated from here
/usr/local/include/boost/asio/impl/read.hpp:215: error: no match for call to ‘(boost::function<void ()(long unsigned int)>) (const boost::system::error_code&, const long unsigned int&)’
/usr/local/include/boost/function/function_template.hpp:1007: note: candidates are: typename boost::function1<R, T1>::result_type boost::function1<R, T1>::operator()(T0) const [with R = void, T0 = long unsigned int]
make: *** [src/communicator/protocol/Command.o] Error 1
这是我的课:命令.hh
namespace communicator {
namespace protocol {
namespace in {
class Command : public boost::enable_shared_from_this<Command> {
public:
~Command();
typedef boost::shared_ptr<Command> pointer;
void got_newline();
protected:
Command(tcp::socket& socket, structure::Client& client) :
m_socket(socket), m_client(client) {};
void endParsing();
tcp::socket& m_socket;
structure::Client& m_client;
char m_newline[2];
private:
};
}
}
命令.cc:
namespace communicator {
namespace protocol {
namespace in {
void Command::endParsing() {
boost::function<void()> cb = boost::bind(&Command::got_newline,
shared_from_this());
boost::asio::async_read(m_socket,
boost::asio::buffer(m_newline, 2),
boost::asio::transfer_at_least(2),
**ERROR POINTING THIS LINE** cb);
}
void Command::got_newline() {
if (m_newline[0] == 'r' && m_newline[1] == 'n') {
std::cout << "End" << std::endl;
}
}
}
}
}
检查代码块上的"**指向此行时出错**",这是它有问题的地方。。。不知道为什么,一次又一次地打断我的头。。。
感谢的帮助
为了清晰起见,我删除了一些代码,如果您有任何问题,请不要犹豫
您的完成处理程序签名不正确,请考虑以下示例
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
void
foo()
{
}
int
main()
{
boost::asio::io_service io_service;
boost::asio::ip::tcp::socket socket( io_service );
char buf[2];
// this compiles file
boost::asio::async_read(
socket,
boost::asio::buffer(buf),
boost::asio::transfer_at_least(2),
boost::bind( &foo )
);
// this does not
boost::function<void()> cb = boost::bind( &foo );
boost::asio::async_read(
socket,
boost::asio::buffer(buf),
boost::asio::transfer_at_least(2),
cb
);
}
boost::bind
足够聪明,不会将error
或bytes_transferred
参数传递给绑定函数指针。Asio库的作者有一篇关于使用绑定库的详细博客文章。值得一读。
async_*操作需要回调函数的不同签名:
void handler(
const boost::system::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes read.
);
有关如何编写和调用此类回调处理程序的示例,请深入查看文档。
相关文章:
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 在其他容器中使用 boost::container::static_vector 时,GCC 编译错误"将'const s'绑定到类型's&'的引用丢弃限定符"
- 编译器如何在编译时执行数据绑定?
- 为什么 std::bind 绑定到成员函数时无法编译?
- 为什么我无法编译结构化绑定?
- 在 Mac 上C++编译代码时处理“dyld:惰性符号绑定失败:找不到符号”错误
- std::绑定到 lambda: 编译错误
- 在编译时初始化一个C++结构,就像一个未知绑定的数组
- 无法编译 C++ 17 个结构化绑定
- 为 OpenCV 编译 MATLAB 绑定
- 编译时绑定C++模板
- 使用绑定C 时,Python编译错误
- 将规则生产绑定到我的结构成员时出现编译错误
- 有没有一种方法可以从python类生成一个c++类,并在编译时将其绑定
- 使用 mingw 和 msis 编译带有 Python 绑定的 c++ 时出错
- boost::绑定不要使用成员模板函数编译
- 编译错误与 std::find_if 中的提升绑定
- 双加速::绑定导致编译时错误
- 编译错误无法将'std::basic_ostream<char>' lvalue 绑定到'std::basic_ostream<char>&&'
- boost::asio::async_read绑定编译错误