I have a class, with attributes that I define like this:
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;
And the constructor implementations, that i define like this :
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))
{}
Here, in the constructor, I define almost all the attributes like they need to be defined.
But I have a problem that I never had in a full .cpp
file: When I define the resolver in the constructor, the compiler says that I've got an error:
/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)){
^
I don't understand why, because I use the regular things!
Any ideas?
PS : If i try to remove the &
from m_io_service
, i get even more errors :
/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)){
AND
/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
Adding to @Muscampester's answer, it looks like your constructor is being redundant.
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))
{}
Should be written like this:
//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)
{}
The difference being that your resolver, query, and socket objects are no longer being "move-constructed", and are instead being directly constructed. Based on the second error you're getting, it looks like you're using a version of boost where those objects are not movable/move-constructable (or possibly boost never intended for these types to be movable; not sure which).
If you're using an older version of boost, try updating to a later version of the library. Whether you do or don't, though, you'll need to update the code so you're no longer needlessly move-constructing.
The constructor for boost::asio::ip::basic_resolver
accepts the parameter by reference:
basic_resolver(boost::asio::io_service& io_service)
and you've taken the address of the variable and passed a pointer:
m_resolver(tcp::resolver(&m_io_service))
Try removing the &
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.