2014-03-30 88 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 

等在其他類