Originally Posted by
kargo
Looks like #ifndef don't work in other headers?
Yes, they do, but they won't work as you expect in this case. Consider the code:
A.h:
Code:
#ifndef TEMP_20110316_1259
#define TEMP_20110316_1259
#pragma message( "Pre-include: Including A.h..." )
#include "Temp2.h"
#pragma message( "Post-include: Including A.h..." )
class A
{
B b;
};
#endif
B.h:
Code:
#ifndef TEMP2_20110316_1259
#define TEMP2_20110316_1259
#pragma message( "Pre-include: Including B.h..." )
#include "Temp.h"
#pragma message( "Post-include: Including B.h..." )
class B
{
A a;
}
#endif
When compiling, I get this output:
2> Pre-include: Including A.h...
2> Pre-include: Including B.h...
2> Post-include: Including B.h...
error C2146: syntax error : missing ';' before identifier 'a'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2> Post-include: Including A.h...
error C2236: unexpected 'class' 'A'. Did you forget a ';'?
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)
What is happening?
A.h includes B.h.
B.h includes A.h.
B.h tries to include A.h. But the define has been defined, so it skips including its contents.
Here is the problem. Because A.h included B.h before the actual contents of the file, the declaration of A is missing at this point.
B.h assumes it has the declaration of A at this point and tries to use it, but it doesn't exist, so we get an error.
Well so i put
Code:
class NetworkHandler;
extern NetworkHandler NHandler;
in my header, then i need to initilialize it somehow in share.cpp ?
Yes, you must put
NetworkHandler NHandler;
inside a .cpp file. Any source file.