The problem is with the first line of your header file #ifdef _BASE_H_
. It should be #ifndef _BASE_H_
(notice the extra 'n')
Here is a brief explanation of what is happening:
#ifdef MACRO_NAME
<line 1>
<line 2>
...
#endif
This code tells the C++ preprocessor to only include the line1, line2, ... in the source code if a macro MACRO_NAME
has been defined prior to this point (Note: It's just the definition of the macro which matters, not it's value. If you want to predicate code inclusion on macro value, you may use #if
)
The technique you are trying to use here is called Include Guard, and it safeguards against the duplicate inclusion of code (mostly by #include
preprocessor directive). The idea is include source code of a header file only if some macro (say "M") has not been defined yet, and then inside the header file code the first thing you do is define that macro "M" (so that next call to #include samefile
will not include its code again). For creating a Include guard you need to use the #ifndef
preprocessor directive (which is the exact opposite of #ifdef
, in fact, the extra n stands for "NOT").
Since you incorrectly used #ifdef
, the file base.h
was never included (as the only place you define the macro _BASE_H_
is after checking that it is already defined!). Thus the compiler does not know about the class base
, leading to the error expected class-name before ‘{’ token