我有一个类,我定义的属性如下:
private:
const std::string m_ip;
bool is_connected;
boost::asio::io_service m_io_service;
tcp::resolver m_resolver;
tcp::resolver::query m_query;
tcp::resolver::iterator m_endpoint_iterator;
tcp::socket m_socket;
构造函数实现,我定义如下:
Connection::Connection(const std::string ip)
: m_ip(ip)
, is_connected(false)
, m_resolver(tcp::resolver(&m_io_service))
, m_query(tcp::resolver::query(m_ip, "connect_back"))
, m_endpoint_iterator(m_resolver.resolve(m_query))
, m_socket(tcp::socket(m_io_service))
{}
在构造函数中,我定义了几乎所有需要定义的属性。
但我有一个问题,我从来没有在一个完整的.cpp
文件中:当我在构造函数中定义解析器时,编译器说我有一个错误:
/home/User/C++ Projects/Client/Network.cpp:9: error: no matching function for call to ‘boost::asio::ip::basic_resolver<boost::asio::ip::tcp>::basic_resolver(boost::asio::io_service*)’
Connection::Connection(const std::string ip) : m_ip(ip), is_connected(false), m_resolver(tcp::resolver(&m_io_service)), m_query(tcp::resolver::query(m_ip, "connect_back")), m_endpoint_iterator(m_resolver.resolve(m_query)), m_socket(tcp::socket(m_io_service)){
^
我不明白为什么,因为我使用常规的东西!
有什么想法吗?
PS:
如果我尝试从&
删除m_io_service
,我会收到更多错误:
/home/User/C++ Projects/Client/Network.cpp:9: error: use of deleted function ‘boost::asio::ip::basic_resolver<boost::asio::ip::tcp>::basic_resolver(boost::asio::ip::basic_resolver<boost::asio::ip::tcp>&&)’
Connection::Connection(const std::string ip) : m_ip(ip),
is_connected(false),
m_resolver(tcp::resolver(m_io_service)),
m_query(tcp::resolver::query(m_ip, "connect_back")),
m_endpoint_iterator(m_resolver.resolve(m_query)),
m_socket(tcp::socket(m_io_service)){
和
/usr/include/boost/asio/ip/basic_resolver.hpp:44: error: ‘boost::asio::basic_io_object<IoObjectService, Movable>::basic_io_object(const boost::asio::basic_io_object<IoObjectService, Movable>&) [with IoObjectService = boost::asio::ip::resolver_service<boost::asio::ip::tcp>; bool Movable = false]’ is private within this context
答案 0 :(得分:2)
添加@ Muscampester的答案,看起来你的构造函数是多余的。
Connection::Connection(const std::string ip)
: m_ip(ip)
, is_connected(false)
, m_resolver(tcp::resolver(&m_io_service))
, m_query(tcp::resolver::query(m_ip, "connect_back"))
, m_endpoint_iterator(m_resolver.resolve(m_query))
, m_socket(tcp::socket(m_io_service))
{}
应该这样写:
//I changed this to const& because, while Copy Ellision *might* optimize this, it's still
//more semantically correct to pass by const reference.
Connection::Connection(const std::string & ip)
: m_ip(ip)
, is_connected(false)
//Note that we're no longer referring to the underlying type.
, m_resolver(m_io_service)
, m_query(m_ip, "connect_back")
, m_endpoint_iterator(m_resolver.resolve(m_query))
, m_socket(m_io_service)
{}
不同之处在于您的解析器,查询和套接字对象不再是&#34;移动构造的&#34;,而是直接构造。根据你得到的第二个错误,看起来你正在使用一个版本的boost,其中这些对象不可移动/可移动构造(或者可能提升永远不会使这些类型可移动;不确定其中)。
如果您使用较旧版本的boost,请尝试更新到该库的更高版本。不管你做了什么,或者不做,你都需要更新代码,这样你就不再需要不必要的移动构建了。
答案 1 :(得分:1)
boost::asio::ip::basic_resolver
的{{3}}通过引用接受参数:
basic_resolver(boost::asio::io_service& io_service)
你已经获取了变量的地址并传递了一个指针:
m_resolver(tcp::resolver(&m_io_service))
尝试删除&
。