我对c ++很陌生,并尝试编写简单的boost :: asio服务器包装类,并且遇到一些麻烦。按照这个例子http://thisthread.blogspot.ru/2011/02/minimal-asio-tcp-server.html,并尝试将asio对象放在我的类的对象中:
tcpserver.h:
class tcpserver {
public:
tcpserver(unsigned short port = 9999);
~tcpserver();
private:
boost::asio::io_service io_service;
boost::asio::ip::tcp::endpoint endpoint;
boost::asio::ip::tcp::acceptor acceptor;
boost::asio::ip::tcp::socket socket;
};
tcpserver.cpp:
#include "tcpserver.h"
tcpserver::tcpserver(unsigned short port){
endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), port);
acceptor = boost::asio::ip::tcp::acceptor(io_service, endpoint);
}
编译器抛出以下错误:
/Users/rusik/huyorg/lib/tcpserver/tcpserver.cpp:3:12: error: constructor for 'tcpserver' must explicitly initialize the member 'acceptor' which does not have a default
constructor
tcpserver::tcpserver(unsigned short port){
我该如何正确地做到这一点?谢谢!
答案 0 :(得分:1)
在初始化中使用它:
tcpserver::tcpserver(unsigned short port) :
endpoint(boost::asio::ip::tcp::v6(), port),
acceptor(io_service, endpoint)
}
因为当你按自己的方式行事时,成员是临时默认构建的(接受者缺少这些成员)。 在这里,他们直接用正确的参数构建