由于包括警卫,类型不完整

时间:2011-10-30 21:45:34

标签: c++

我正在尝试编译一个项目,我遇到了一个不完整类型的问题。以下是make的输出:

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

谢谢!

3 个答案:

答案 0 :(得分:3)

您不能使用不完整类型声明字段。你必须改为指定一个指针。

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

否则,您必须在第一个标题中包含客户端通信标头。

ifndef NAMING_STUB_H__
#define NAMING_STUB_H__

#include "ClientCommunication.h"

答案 1 :(得分:1)

ClientCommunication应在Naming_stub.h声明之前在Naming_stub中完全声明,或其clientCommunication成员应成为指针/引用。只有那些工作不完整的类型。类似建议适用于Endpoint中的ClientCommunication.h

答案 2 :(得分:1)

据我所知,你没有#include“ClientCommunication.h”或#include“RemoteTypes.h”

所以,在ClientCommunication.h中#include“RemoteTypes.h”,在Naming_stub.h中#include“ClientCommunication.h”