无法从“const boost::asio::const_buffers_1”转换到“boost::asio::mutable_buffer”
bool Connection::Receive(){
boost::shared_ptr<std::string> buffer(new std::string());
socket_.async_receive(boost::asio::buffer(*buffer), boost::bind(&Connection::handler, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
//std::cout<<buffer<<std::endl;
int recvlen = buffer->length();
if (recvlen <= 0) {
return false;
}
//this->OnReceived(buffer, recvlen);
return true;
}
错误:
Error 1 error C2440: '<function-style-cast>' : cannot convert from 'const boost::asio::const_buffers_1' to 'boost::asio::mutable_buffer' e:\boost_1_46_1\boost_1_46_1\boost\asio\detail\buffer_sequence_adapter.hpp 211
bool Connection::Receive(){
boost::shared_ptr<std::string> buffer(new std::string());
socket_.async_receive(boost::asio::buffer(*buffer), boost::bind(&Connection::handler, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
//std::cout<<buffer<<std::endl;
int recvlen = buffer->length();
if (recvlen <= 0) {
return false;
}
//this->OnReceived(buffer, recvlen);
return true;
}
Error:
Error 1 error C2440: '<function-style-cast>' : cannot convert from 'const boost::asio::const_buffers_1' to 'boost::asio::mutable_buffer' e:\boost_1_46_1\boost_1_46_1\boost\asio\detail\buffer_sequence_adapter.hpp 211
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
async_receive
函数需要更改缓冲区的内容,但不能更改常量缓冲区。使用
mutable_buffer
而不是常量缓冲区。您可以在此处您需要将指针和大小传递给可变缓冲区:
在全局范围内声明
content_buffer
非常重要,因为async_receive
函数正在异步访问缓冲区。如果您在函数中声明缓冲区,则它会在接收数据之前超出范围。The
async_receive
function need to change the content of the buffer, but you can't change a constant buffer.Use a
mutable_buffer
instead of a constant buffer. You can find some informations hereYou need to pass a pointer and the size to the mutable buffer:
It's important that you declare your
content_buffer
in a global scope, bacause theasync_receive
function is accessing the buffer async. If you declare your buffer in the function, it go out of scope before data get received.