我有以下代码使用Boost ASIO来设置TCP客户端。这里是我的代码改编自Boost doc的chat example。包括boost/thread.cpp时编译错误
class AsioCommunicationService {
AsioCommunicationService::AsioCommunicationService(
boost::asio::io_service& io_service,
tcp::resolver::iterator endpoint_iterator)
: io_service_(io_service),
socket_(io_service)
{
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
boost::bind(&AsioCommunicationService::handle_connect, this,
boost::asio::placeholders::error, ++endpoint_iterator));
}
void AsioCommunicationService::handle_connect(const boost::system::error_code& error,
tcp::resolver::iterator endpoint_iterator)
{
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
{
//init io_service, query, resolve, iterator here
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(host, service);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
resolver.resolve(query);
m_session = std::shared_ptr<AsioCommunicationService>(
new AsioCommunicationService(io_service, iterator));
//start new thread for io_service.run --> GOT AN ERROR when include boost/thread.hpp
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
//this synchronous command would work, but it's blocking the program. I don't want that.
//io_service.run();
}
当然,我需要包含boost/thread来声明类Connection的变量t。但是,当我这样做,我得到这个错误
#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 (*)()]’
这将编译,如果我删除包括推动/ thread.hpp,并更换声明通过简单的调用吨至io_service.run工作() ;我想知道这个编译错误是否与boost版本有关。如果有任何帮助,我使用Boost ASIO 1.42,Ubuntu 11.04和Eclipse。先谢谢你。
你可以尝试去掉一个小的自包含的例子,让人们可以试验它吗? – MvG 2012-07-30 22:07:26
Boost 1.42相当过时(从2010年afaics)。至少有一些错误消息引用了右值引用,这些引用是C++ 11标准的一部分,并且在那个时候肯定已经非常流行。可能还有一些粗糙的边缘。我会尝试最新版本的boost,看看是否能解决您的问题。 – MvG 2012-07-30 22:19:19
感谢@MvG,基本上当我把include放入我的代码时,我得到了那些错误。其余的并不关心我的想法。 –
2012-07-31 08:12:14