Asio async_read_some模板错误
我正在使用非boost asio编写一个简单的异步服务器。我遇到了一个巨大的模板错误,坦率地说,我甚至无法想象它是什么意思。Asio async_read_some模板错误
仅供参考,我的代码看起来非常像http://think-async.com/Asio/asio-1.5.3/src/examples/http/server3/connection.cpp。
这里是我的代码:
void client_connection::serve()
{
// std::cout << "Began serving connection" << std::endl;
// refer to http server 3 connection.cpp
this->socket_.async_read_some(asio::buffer(&protocol_version_, 1), this->strand_.wrap(std::bind(&client_connection::handle_read, this->shared_from_this(), asio::placeholders::error, asio::placeholders::bytes_transferred)));
// 65535 max tcp size
}
void client_connection::handle_read(const asio::error_code& error_, std::size_t bytes_)
{
if (error_) return;
// for debug: print out all the recieved bytes
std::cout << std::hex << protocol_version_ << " " << std::endl;
this->serve();
}
以下是错误:
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(1152): error : no instance of overloaded function "std::_Pmf_wrap<_Pmf_t, _Rx, _Farg0, _V0_t, _V1_t, std::_Nil, std::_Nil, std::_Nil, std::_Nil, std::_Nil>::operator() [with _Pmf_t=void (client_connection::*)(const asio::error_code &, size_t={unsigned int}), _Rx=void, _Farg0=client_connection, _V0_t=const asio::error_code &, _V1_t=size_t={unsigned int}]" matches the argument list
1> argument types are: (client_connection *, boost::arg<1>, boost::arg<2>)
1> object type is: std::_Pmf_wrap<void (client_connection::*)(const asio::error_code &, size_t), void, client_connection, const asio::error_code &, size_t, std::_Nil, std::_Nil, std::_Nil, std::_Nil, std::_Nil>
1> _VARIADIC_EXPAND_0X(_CLASS_BIND, , , ,)
1> ^
(+~20 not very relevant lines)
我有一种预感,这是关系到shared_from_this,但发生错误与否我删除shared_from_this行为。
任何形式的帮助非常感谢。
我认为asio::placeholders::*
与boost::bind
一起使用,但我不确定它们是否与std::bind
一起使用。改为std::placeholders::_1
和std::placeholders::_2
,或使用boost::bind
。
后者 - 更改为:: _ 1和:: _ 2,工作。 – kvanberendonck 2013-04-07 12:43:12
“你必须绑定这个指针才能调用成员函数,而不是shared_ptr” - 这是不正确的语句。 – 2013-04-07 12:59:11
糟糕,你是对的。我删除了不正确的陈述。谢谢,我也学到了一些东西。 – rhashimoto 2013-04-07 13:06:39
'asio :: placeholders :: bytes_transferred'的类型是什么?它可以转换为'std :: size_t'吗? – 2013-04-07 12:04:01
另外,“strand_”的类型是什么? – 2013-04-07 12:06:23
strand_的类型是asio :: io_service :: strand和asio :: placeholders :: bytes_transferred是一些我不太了解的魔法。对于占位符::错误和占位符:: bytes_transferred请参阅http://stackoverflow.com/questions/15859605/whats-the-use-of-asioplaceholdererror – kvanberendonck 2013-04-07 12:11:50