Nested "include"???

Hi,

I am confused of how to use nested "include"s, since I wrote some and g++ gave me several errors saying that I used undefined classes and " error: forward declaration of". However, I already declare those classes in other files. My real codes are quite long but it basically looks like the following. I wonder whether I placed some definition where I am not supposed to and there is any better ways to it?

Thanks,

/////File1.h/////
#ifndef FILE1_H
#define FILE1_H
class A;
class B;

#include "file2.h"
class A
{
B b;
.......
};

class B
{
C c;
.......
};
#endif

////File2.h//////
#ifndef FILE2_H
#define FILE2_H

class C;

#include "file1.h"
class C
{
A x;
.....
}

class D
{
B y;
......
}
#endif

Your design pattern is screwed up.

A has a B which has a C which has an A...you can't do that.
Oh Actually I meant for a, b, c, x and y to be pointers. They all will form some kinds of multi-dimension linked lists. Like for example "b" of an A instance will point to an instance of B. Then, each instance of B contains *next which points to another instance of B and so on.
Also, each instance of B contains c that points to an instance of C. Also, each instance of C contains *next which points to another instance of C and so on.

For each instance of C, x will just point to an instance of A. (not form a linked list for x)

I know it might look weird but I just happened did it. Now I have two header files like above. Each of them contains the other header like file1.h includes file2.h and file2.h also includes file1.h.
I am wonder whether these "include"s are valid?
Last edited on
Relevent Link:

http://cplusplus.com/forum/articles/10627/

See The "Right Way" to Include
Thanks, Disch! That helps! I changed the include statement to forward-declaration, then it works!
Thank you very much.
Topic archived. No new replies allowed.