Head File include recursively...


1
2
3
4
5
class CFather
{
CFather() {}
virtual ~CFather() {}
};

is defined in file father.h


1
2
3
4
5
class CSon1 : public CFather
{
CSon1() {}
virtual ~CSon1() {}
};

is defined in file son1.h


1
2
3
4
5
class CSon2 : public CFather
{
CSon2() {}
virtual ~CSon2() {}
};

is defined in file son2.h

Apparently, both son1.h and son2.h must have the following line:
#include "father.h"

However, if meanwhile, there is a function is CFather:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void func(int type)
{
...
   switch( type)
   {
      case 1: 
      {
      CFather* father = new CSon1();
      ...
      }
      case 2:
      {
      CFather* father = new CSon2();
      ...
      }
   }
...
}



In this case, recursive include seems to be a must.
How to avoid those compilation error reports?

Thank you very much.

Best Regards
JIA
For every class create a .h file and a .cpp file. In .h you declare the class and in .cpp you define the its methods. That way you will only need to include son1.h and son2.h into father.cpp and that won't cause any errors.
There is a good article about including: http://www.cplusplus.com/forum/articles/10627/

Perfect !!
Thank you hamsterman !!

It's now done !!
Thank you !!

Best Regards
JIA
Topic archived. No new replies allowed.