Click to See Complete Forum and Search --> : Multi include


February 22nd, 2000, 08:01 AM
Hi I have a little include problem. I will try to explain it as good as I can. Here is some code from a class:

// test.h

#ifndef .....
#define ...

#include <macrodef.h>
#include <stuff.h>

class test {
public:
...
private:
SOME_STRUCT *struct;
};

#endif




SOME_STRUCT is defined in stuff.h and therefore stuff.h is needed in this file.
The thing is the code to this is placed in "test.cpp" which is including "test.h". So far so good. The problem is that "stuff.h" is defining a macro of a macro defined in "macrodef.h". That new macro is needed in "test.cpp". If I compile this VC++ complins anut that this macro is aldready defined in the start file wich includes "test.h".
It seems that "macrodef.h" must only be included once. Sice the macro is only used in "test.cpp" I would like to include it in there but I must include "macrodef.h" before "stuff.h" and I need that file in "test.h".
Do you understand my problem? Is there a good way to solve this?

Excuse me for my language..

/ Anders

ALM
February 22nd, 2000, 09:36 AM
I'm not too clear on what your problem is but I have two recommendations:

1. You only need to include a header file if the compiler needs specific information about the class or struct your using. In the case of test.h, since you're declaring just a pointer to SOME_STRUCT, then all the compiler needs to know is that it's a struct. The compiler doesn't care about its size or its members. Just telling it it's a struct is enough, so just "forward" declare it at the top of test.h like this:

struct SOME_STRUCT;

Now you don't have to include stuff.h in your test.h, but you will need it inside test.cpp.

2. You should make your header files as self-contained as possible. In other words, if stuff.h requires something that's defined in macrodef.h then include macrodef.h inside stuff.h. This allows anyone who needs to use stuff.h to not have to worry about other header files that stuff.h needs.

I hope to have helped. Good luck!
Alvaro