我发现,如果我想boost::socket
用作类成员,我必须使用初始化列表来定义它,并且必须在构造函数调度之前定义它。
这意味着,我必须用一些虚拟参数证明它的构造函数,当我有我需要的数据时,我需要再次重新初始化它。
客户端.hpp
class SocketClient {
private:
int port; //Port, currently unused
boost::asio::io_service io_service; //This is my "dummy" parameter
boost::asio::ip::tcp::resolver::iterator endpoint_iterator;
boost::asio::ip::tcp::socket sock; //Socket class instance
public:
void init(const char*, const char* );
SocketClient::SocketClient():sock(io_service){}; //Giving empty "io_service" to the "sock"
bool read(int bytes, char *text);
bool send(int length, char *text);
bool send(std::string text);
unsigned int timeout;
};
稍后,当init()
调用方法时,我需要提供sock
with initialized io_service
。我不知道该怎么做:
客户端.h
void SocketClient::init(const char* ip_a, const char* port_a) {
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(ip_a, port_a);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
//Error (unsuported operator =):
sock=boost::asio::ip::tcp::socket(io_service);
//Error (term does not evaluate to a function taking 1 arguments):
sock(io_service);
}
在这里您可以看到整个项目,没有任何内容被删除。