我对C ++还是很陌生,所以请不要评判我:-)
我有一个connection
包含以下方法的类:
void connection::handle_connect(const boost::system::error_code& err) {
if (!err) {
boost::asio::async_write(socket_, request_,
boost::bind(&connection::handle_write_request, this,
boost::asio::placeholders::error, &connection::handle_read_status_line));
} else {
std::cout << "Error in handle_connect: " << err.message() << "\n";
}
}
void connection::handle_write_request(const boost::system::error_code& err, boost::function<void(const boost::system::error_code&)> callback) {
if (!err) {
boost::asio::async_read_until(socket_, response_, 0,
boost::bind(&callback, this,
boost::asio::placeholders::error));
} else {
std::cout << "Error in handle_write_request: " << err.message() << "\n";
}
}
void connection::handle_read_status_line(const boost::system::error_code& err) {
if (!err) {
std::istream response_stream(&response_);
std::string response;
std::getline(response_stream, response);
std::cout << "Response: " << response << std::endl;
} else {
std::cout << "Error in handle_read_status_line: " << err << "\n";
}
}
并且我正在尝试使该handle_write_request
方法更具通用性。我已经添加callback
了它的签名-这应该是传递给boost::bind
并在中作为回调调用的方法的地址boost::asio::async_read_until
。
但是,这甚至不能编译:-) MSVS2013在说
Error 1 error C2825: 'F': must be a class or namespace when followed by '::' C:\local\boost_1_55_0\boost\bind\bind.hpp 69 1 driver
Error 2 error C2039: 'result_type' : is not a member of '`global namespace'' C:\local\boost_1_55_0\boost\bind\bind.hpp 69 1 driver
Error 3 error C2146: syntax error : missing ';' before identifier 'type' C:\local\boost_1_55_0\boost\bind\bind.hpp 69 1 driver
Error 4 error C2208: 'boost::_bi::type' : no members defined using this type C:\local\boost_1_55_0\boost\bind\bind.hpp 69 1 driver
Error 5 error C1903: unable to recover from previous error(s); stopping compilation C:\local\boost_1_55_0\boost\bind\bind.hpp 69 1 driver
如何正确传递成员函数的地址?如有任何建议,我将不胜感激!
要绑定类的成员函数,请执行以下操作:
boost::bind(&SomeClass::SomeMemberFunc, this, params...);
就您而言,callback
已经是一个函子,所以请尝试boost::bind(callback, this, <some proper parameter>);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句