1

I am trying to get a project to compile, and I am running into a problem with incomplete types. Here is the output from 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

Here are my header files in question:

In 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

In 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__      

Finally, there is the ClientCommunication.h file:

#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__

Sorry that this is so long, but there is one more header file. The Endpoint struct is declare in RemoteTypes.h, which looks like the following:

#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

Thanks!

Max
  • 15,157
  • 17
  • 82
  • 127
  • Don't use double-underscores in your identifiers, [they're reserved](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – GManNickG Oct 30 '11 at 22:08

3 Answers3

3

You cannot declare a field using an incomplete type. You have to declare a pointer instead.

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

Otherwise, you have to include the client communication header in the first header.

ifndef NAMING_STUB_H__
#define NAMING_STUB_H__

#include "ClientCommunication.h"
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
1

ClientCommunication should be fully declared in Naming_stub.h before the declaration of Naming_stub, or its clientCommunication member should become a pointer/reference. Only those work with incomplete types. Similar advice applies to Endpoint in ClientCommunication.h.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1

As far as I can tell, you don't have #include "ClientCommunication.h" or #include "RemoteTypes.h" anywhere

So, #include "RemoteTypes.h" in ClientCommunication.h and #include "ClientCommunication.h" in Naming_stub.h

Joshua Clark
  • 1,346
  • 3
  • 14
  • 24