2011-10-30 34 views
1

我想獲得一個項目來編譯,我遇到了不完整類型的問題。下面是使輸出:類型是不完整的,因爲包括警衛

In file included from ../common/ClientCommunication.h:13, 
       from EchoService_stub.h:9, 
       from EchoService_stub.cpp:1: 
../common/Naming_stub.h:16: error: field ‘clientCommunication’ has incomplete type 
In file included from EchoService_stub.h:9, 
       from EchoService_stub.cpp:1: 
../common/ClientCommunication.h:20: error: field ‘serverEndpoint’ has incomplete type 
make: *** [EchoService_stub.o] Error 1 

這裏有問題我的頭文件:

在Naming_stub.h:

ifndef NAMING_STUB_H__ 
#define NAMING_STUB_H__ 

#include <string> 
#include <string.h> 
#include "Naming.h" 

class ClientCommunication; 

class Naming_stub : public Naming { 

private: 
    ClientCommunication clientCommunication; 

protected: 
    // Protected Methods Here. 

public:          
    // Public Methods Here. 
}; 
#endif 

在Naming.h

#ifndef NAMING_H__ 
#define NAMING_H__ 

#include "Remote.h" 
#include "../util/RemoteException.h" 

class Naming { 
private: 
protected: 
public: 

    virtual Remote* lookup(std::string lookupURL) throw (RemoteException); 

    virtual void bind() throw (RemoteException); 

    ~Naming(); 
}; 

#endif // NAMING_H__  

最後,有ClientCommunication.h文件:

#ifndef CLIENT_COMMUNICATION_H__ 
#define CLIENT_COMMUNICATION_H__ 

#include <iostream> 
#include <cstdlib> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include "../util/RemoteTypes.h" 
#include "Naming_stub.h" 

struct Endpoint; 

class ClientCommunication { 
private: 
    int socket_fd; 
    struct Endpoint serverEndpoint; 

protected: 

public: 
    ClientCommunication(const struct Endpoint& serverEndpoint_in); 
    struct Buffer send(struct Buffer& callBuffer); 
    ~ClientCommunication(); 
}; 

#endif // CLIENT_COMMUNICATION_H__ 

對不起,這是如此之久,但還有一個頭文件。 Endpoint結構聲明在RemoteTypes.h中,如下所示:

#ifndef REMOTE_EXCEPTION_H__ 
#define REMOTE_EXCEPTION_H__ 

#include <netinet/in.h> 

struct Endpoint { 
    int port; 
    char* service_id; 
    char* server_ip; 
    struct sockaddr_in host_name; 
}; 

struct Buffer { 
    char* contents; 
    int size; 
}; 

#endif 

謝謝!

+0

不要在您的標識符中使用雙下劃線,[它們是保留](http://stackoverflow.com/questions/228783/what-are -the-規則約-使用-AN-下劃線在-AC-標識符)。 – GManNickG

回答

3

您不能使用不完整的類型聲明一個字段。你必須聲明一個指針。

private: 
    ClientCommunication clientCommunication; // requires #include 
    ClientCommunication *clientCommunication; // works with incomplete type 

否則,您必須在第一個標頭中包含客戶端通信標頭。

ifndef NAMING_STUB_H__ 
#define NAMING_STUB_H__ 

#include "ClientCommunication.h" 
1

ClientCommunication應該在Naming_stub的聲明之前在Naming_stub.h中完全聲明,或者其clientCommunication成員應該成爲指針/引用。只有那些工作類型不完整。類似的建議適用於EndpointClientCommunication.h

1

據我所知,你不必在ClientCommunication的#include 「ClientCommunication.h」 或#包括 「RemoteTypes.h」 隨處

所以,#包括 「RemoteTypes.h」 .h和#include「ClientCommunication.h」在Naming_stub.h中