gpt4 book ai didi

c++ - Asio::async_connect 和 io_service.run 不编译

转载 作者:行者123 更新时间:2023-11-28 03:33:50 25 4
gpt4 key购买 nike

我正在学习使用 Boost ASIO。这是我的代码改编自 Boost 文档的 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”的成员。显然,就 IDE 告诉我的而言,async_connect 不是 boost asio 的成员,而是 socket 的成员。我正在使用 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 签名以包含迭代器参数后,我成功编译了该行。但是,编译器现在停留在包含到 thread.hpp

#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(&boost::asio::io_service::run, &io_service)) 来启动新线程; ,即使我通过对 io_service.run() 的简单调用替换它也能正常工作;

最佳答案

因为你的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));

你的最后一个参数是错误的,所以,正确的绑定(bind)是

boost::bind(&AsioCommunicationService::handle_connect, this,
boost::asio::placeholders::error));

关于c++ - Asio::async_connect 和 io_service.run 不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11718597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com