I just keep running
into problems (Windows platform) because files are included in the wrong
order,
Headers should be minimal and complete. Each header should include every
header it needs. Nothing more. This would prevent users needing to know
about inclusion orders.
One cheap trick to ensure the completeness of headers is to include that
header in that headers implementation file *first*. Assume foo.h exists,
then in foo.cpp:
// foo.cpp
#include "foo.h" // before other includes
Some Windows headers were not complete (at least in the VC++ 6.0 days),
so you were forced to know some inclusion order.
one include file messes up the other one etc..
That sounds like a macro problem. One macro defined in one header
changes the text in another header. Macros are evil; try to avoid them.
One of the things however I'm constantly trying to do is to
prevent header files from being included more than once
This is needed and important for each translation unit. Each translation
unit should include each header file that it needs only once.
- do I not need
to worry about this when it's included from different cpp files?
Perfectly normal. The C++ compilation unit is called a translation
unit. A translation unit is what comes out of the
preprocessor. Translation units are not aware of other translation
units. If a cpp file needs definitions in a header, it must include that
header itself.
Also, when I #define macros and put them at the beginning of the header
file like
#ifndef HEADERFILE1_H
#define HEADERFILE1_H
...
..
#endif
then those macro definitions don't seem to stick. When the next cpp
files is being processed, the header file is being included again - even
though the macro was previously defined
Is this normal, and where am I getting confused?
This is normal.
Ali