我有以下代碼使用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