我创建一个套接字类,它有一个静态成员,用于创建不同类型的套接字(客户端和服务器)。这是界面:
class Socket{
public:
int sendData(std::string _data);
std::string receiveData();
int closeSocket();
protected:
Socket();
virtual int initializeSocket() = 0;
virtual int connectSocket() = 0;
public: // static members: Factory, etc.
static Socket* createClientSocket(std::string _ip, std::string _port);
static Socket* createServerSocket(std::string _port);
protected:
WSADATA mWsaData;
SOCKET mSocket;
addrinfo *mResult, mHints;
孩子们:
- >服务器:
class ServerSocket: public Socket{
private:
ServerSocket(const std::string _port);
~ServerSocket();
int acceptClient();
private:
SOCKET mClientSocket;
std::string mPort;
}; // class ServerSocket
- >客户端:
class ClientSocket: public Socket{
private:
ClientSocket(const std::string _ip, const std::string _port);
~ClientSocket();
private:
addrinfo *mPtr;
std::string mServerIp, mServerPort;
}; // class ClientSocket
但是在实现initializeSocket和其他成员时我得到错误“不允许继承的成员。就我而言,继承的类可以访问父类的public和protected memb。为什么会出现这个错误?
提前致谢:)
编辑: 许多成员都会出现错误,例如在ServerSocket.cpp
中int ServerSocket::initializeSocket(){ <<--- HERE in the name of the function appears
// Resolve the server address and port
int iResult = getaddrinfo(NULL, mPort.c_str(), &mHints, &mResult);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
//..... ETC....
freeaddrinfo(mResult);
iResult = listen(mSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(mSocket);
WSACleanup();
return 1;
}
return 0;
}
我在哪里写了“&lt;&lt; - HERE ......”
1>..\..\source\core\comm\ServerSocket.cpp(37): error C2509: 'initializeSocket' : member function not declared in 'BOViL::comm::ServerSocket'
1> c:\programming\bovil\source\core\comm\ServerSocket.h(18) : see declaration of 'BOViL::comm::ServerSocket'
答案 0 :(得分:0)
错误是派生类没有在声明中声明成员函数。
class ClientSocket: public Socket{
public:
int sendData(std::string _data); <<--- Add this
private:
ClientSocket(const std::string _ip, const std::string _port);
~ClientSocket();
int initializeSocket(); <<--- Add this
int connectSocket(); <<--- Add this
private:
addrinfo *mPtr;
std::string mServerIp, mServerPort;
}; // class ClientSocket
等在另一个班级