boost의 오류 중 하나입니다. error C2512

1742 단어
boost   

boost::asio::basic_stream_socket' : no appropriate default constructor available
 
질문 재현:
 
class SocketClient {
private:
    int port;    //Port, currently unused
    boost::asio::io_service io_service;  
    boost::asio::ip::tcp::resolver::iterator endpoint_iterator;  
    boost::asio::ip::tcp::socket sock;  //This causes the error
    //It wants to be like this (impossible too):
  //boost::asio::ip::tcp::socket sock(io_service);
public:
    void init(const char*, const char* );
    SocketClient();  //Default constructor
    bool connect();
    bool read(int bytes, char *text);
    bool send(int length, char *text);
    bool send(std::string text);
    unsigned int timeout;
};
And here is the constructor:

SocketClient::SocketClient() {  //ERROR: (23): error C2512: 'boost::asio::basic_stream_socket<Protocol>' : no appropriate default constructor available
    sock=boost::asio::ip::tcp::socket(io_service);  //Adding this didn't help
}

해결 방법:
SocketClient::SocketClient() 
    :
    sock(io_service)
{
    // Other initialization code here...
}

원인:
Be careful though: this is well-defined because member variables are constructed in the order they appear in the class definition, and  io_service  appears before  sock . If that wasn't the case, you would pass an uninitialized object to the constructor of  socket , most likely resulting in Undefined Behavior.

좋은 웹페이지 즐겨찾기