2014-03-30 108 views
0

林创建具有用于产生不同类型的套接字(客户端和服务器)的静态成员的插座类。下面是接口:CPP插座,传承错误

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 ClientSocket: public Socket{ 
    private: 
     ClientSocket(const std::string _ip, const std::string _port); 
     ~ClientSocket(); 

    private: 
     addrinfo *mPtr; 

     std::string mServerIp, mServerPort; 
    }; // class ClientSocket 

但在实施initializeSocket等成员我得到的错误“继承成员是不允许的。至于我的关注,继承类可以存取权限公众父类的受保护的成员。为什么会出现这种错误?

感谢提前:)

编辑: 错误在许多成员出现,例如在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; 
    } 

我在哪里写的 “< < - 这里......”

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

可能的重复[为什么是继承成员不允许?](http://stackoverflow.com/questions/15117591/why-is-inherited-member-not-allowed) –

+0

我看过那篇文章史蒂夫霍华德,但我无法找到麻烦:/ – Bardo91

回答

0

的错误是,派生类didnt宣布在其声明中的成员函数。

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 

等在其他类