Hairi
Hairi

Reputation: 3715

Typedefinition interchange in two header files

I have two header files that should use typedefs of each other. Here is the situation:

For example:

In header file server.h

typedef struct
{
   int myint;
}ServerSelfData;

typedef struct
{
   ServerSelfData servData;
   ClientSelfData clData;
}SERVER_Data;

Into the second header file called client.h we have:

typedef struct
{
   int myint;
}ClientSelfData;

typedef struct
{
   ClientSelfData clData;
   ServerSelfData servData;
}CLIENT_Data;

So during the compilation there will be error in one of these definitions. In which one depends on which file is included first.

Is there any workaround/solution of this problem?

Upvotes: 2

Views: 46

Answers (2)

Codor
Codor

Reputation: 17605

The definitions of ServerSelfData and ClientSelfData could me moved to separate header files (or one separate header file) which would be included in both server.h and client.h.

Upvotes: 0

Mohit Jain
Mohit Jain

Reputation: 30489

Before these header files, you can declare the structs. Later complete the struct with a name.

typedef struct ServerSelfData ServerSelfData;
typedef struct CLIENT_Data CLIENT_Data;

typedef struct ServerSelfData
{
   int myint;
}ServerSelfData;

Upvotes: 2

Related Questions