我正在努力解决编译时错误,并尽可能地尝试,我没有看到我做错了什么或与文档/示例中列出的处理程序函数签名不同。 (我在Linux上使用Boost 1.41)
请帮我理解错误! (下面包含为代码段)
我的应用程序有对象,其方法是async_ *函数的处理程序。以下是代码段。错误报告在标记为"第58行和第34行的行中,其中我使用boost :: bind
class RPC {
public:
char recv_buffer[56];
void data_recv (void) {
socket.async_read_some (
boost::asio::buffer(recv_buffer),
boost::bind ( &RPC::on_data_recv, this, _1, _2 )
); // **<<==== this is line 58, that shows up in error listing**
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] data recvd" << std::endl;
global_stream_lock.unlock();
} // RPC::data_recv
void on_data_recv (boost::system::error_code& ec, std::size_t bytesRx) {
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] bytes rcvd: " << std::endl;
global_stream_lock.unlock();
data_recv(); // call function that waits for more data
} // RPC::on_data_recv
}; // RPC class def
有一个巨大的错误输出,但相关的行似乎是:
../src/besw.cpp:58: instantiated from here
/usr/include/boost/bind/bind.hpp:385: error: no match for call to ‘(boost::_m fi::mf2<void, RPC, boost::system::error_code&, long unsigned int>) (RPC*&, boost::asio::error::basic_errors&, int&)’
/usr/include/boost/bind/mem_fn_template.hpp:272: note: candidates are: R boost::_mfi::mf2<R, T, A1, A2>::operator()(T*, A1, A2) const [with R = void, T = RPC, A1 = boost::system::error_code&, A2 = long unsigned int]
/usr/include/boost/bind/mem_fn_template.hpp:291: note: R boost::_mfi::mf2<R, T, A1, A2>::operator()(T&, A1, A2) const [with R = void, T = RPC, A1 = boost::system::error_code&, A2 = long unsigned int]
make: *** [src/besw.o] Error 1
当我删除占位符(_1和_2)并且拥有一个没有参数的处理程序时,它会编译并执行而不会出错。这是修改过的代码段。
void data_recv (void) {
socket.async_read_some (
boost::asio::buffer(recv_buffer),
boost::bind ( &RPC::on_data_recv, this )
);
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] data recvd" << std::endl;
global_stream_lock.unlock();
} // RPC::data_recv
void on_data_recv (void) {
...
}
答案 0 :(得分:1)
错误代码不能通过引用获取。按值或按const&amp;:
void on_data_recv(boost::system::error_code/* ec */, size_t /*bytes_transferred*/) {
另外,请考虑使用Asio特定的占位符:
socket.async_read_some(boost::asio::buffer(recv_buffer),
boost::bind(&RPC::on_data_recv, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
还要使用合适的锁定装置。我们是C ++!事情很容易让事情安全,所以为什么不呢?
<强> Live On Coliru 强>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <boost/thread.hpp>
static boost::mutex global_stream_lock;
class RPC {
char recv_buffer[56];
public:
void data_recv() {
socket.async_read_some(boost::asio::buffer(recv_buffer),
boost::bind(&RPC::on_data_recv, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
boost::lock_guard<boost::mutex> lk(global_stream_lock);
std::cout << "[" << boost::this_thread::get_id() << "] data recvd" << std::endl;
global_stream_lock.unlock();
}
void on_data_recv(boost::system::error_code/* ec */, size_t /*bytes_transferred*/) {
{
boost::lock_guard<boost::mutex> lk(global_stream_lock);
std::cout << "[" << boost::this_thread::get_id() << "] bytes rcvd: " << std::endl;
}
data_recv(); // call function that waits for more data
}
boost::asio::io_service service;
boost::asio::ip::tcp::socket socket{service};
}; // RPC class def
int main() {}