我正在学习使用Boost ASIO。以下是我的代码改编自Boost doc chat example。
class AsioCommunicationService {
AsioCommunicationService::AsioCommunicationService(
boost::asio::io_service& io_service,
tcp::resolver::iterator endpoint_iterator)
: io_service_(io_service),
socket_(io_service)
{
boost::asio::async_connect(socket_, endpoint_iterator,
boost::bind(&AsioCommunicationService::handle_connect, this,
boost::asio::placeholders::error));
}
void AsioCommunicationService::handle_connect(const boost::system::error_code& error)
{
if (!error)
{
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.data(), LampMessage::header_length),
boost::bind(&AsioCommunicationService::handle_read_header, this,
boost::asio::placeholders::error));
}
}
}
class Connection
{
m_session = std::shared_ptr<AsioCommunicationService>(
new AsioCommunicationService(io_service, iterator));
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
}
编译时,我收到一条错误消息,指出'async_connect'不是'boost :: asio'的成员。显然,async_connect不是boost asio的成员,而是socket的成员,就IDE而言。我正在使用libasiodev 1.41.3。
我尝试修改对此
的调用tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
boost::bind(&AsioCommunicationService::handle_connect, this,
boost::asio::placeholders::error, ++endpoint_iterator));
//Got this error: instantiated from ‘boost::_bi::bind_t<boost::_bi::unspecified, void (networkService::AsioCommunicationService::*)(const boost::system::error_code&), boost::_bi::list3<boost::_bi::value<networkService::AsioCommunicationService*>, boost::arg<1> (*)(), boost::_bi::value<boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> > > >’
更新: 更改handle_connect签名以包含iterator参数后,我成功编译了该行。但是,编译器现在停留在thread.hpp
的include中#include <boost/thread.hpp>
//ERROR: In function ‘boost::thread&& boost::move(boost::thread&&)’:
///usr/include/boost/thread/detail/thread.hpp:349:16: error: invalid initialization of reference of type ‘boost::thread&&’ from expression of type ‘boost::thread’
//In file included from /usr/include/boost/thread/detail/thread_heap_alloc.hpp:17:0,
// from /usr/include/boost/thread/detail/thread.hpp:13,
// from /usr/include/boost/thread/thread.hpp:22,
// from /usr/include/boost/thread.hpp:13,
// from /home/son/dev/logistics/src/frameworks/networkService/NetworkConnection.cpp:13:
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp: In function ‘T* boost::detail::heap_new(A1&&) [with T = boost::detail::thread_data<void (*)()>, A1 = void (*&)()]’:
///usr/include/boost/thread/detail/thread.hpp:130:95: instantiated from here
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp:24:47: error: cannot bind ‘void (*)()’ lvalue to ‘void (*&&)()’
///usr/include/boost/thread/detail/thread.hpp:43:13: error: initializing argument 1 of ‘boost::detail::thread_data<F>::thread_data(F&&) [with F = void (*)()]’
由于某种原因,我无法使用调用boost :: thread t(boost :: bind(&amp; boost :: asio :: io_service :: run,&amp; io_service))启动一个新线程; ,即使它通过简单调用io_service.run();
替换它也可以答案 0 :(得分:2)
因为你的cb有签名
void AsioCommunicationService::handle_connect(const boost::system::error_code& error)
此次通话不正确。
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
boost::bind(&AsioCommunicationService::handle_connect, this,
boost::asio::placeholders::error, ++endpoint_iterator));
你的最后一个标准是错的,所以,右边绑定是
boost::bind(&AsioCommunicationService::handle_connect, this,
boost::asio::placeholders::error));