error with including header multiple times

I've got a program with multiple classes, class A uses classes B and C, and classes B and C both use class D. However when I run it there is a problem with redefinitions. Is it because I've included class D twice effectively? It works fine if I use B but not C, and vice versa, but it doesn't compile when i try to use both.

1
2
3
4
5
6
7
8
9
10
11
#ifndef A_H
#define A_H

#include "B.h"
#include "C.h"

Class A:
{
...
};
#endif 

1
2
3
4
5
6
7
8
9
10
#ifndef B_H
#define B_H

#include "D.h"

Class B:
{
...
};
#endif 

1
2
3
4
5
6
7
8
9
10
#ifndef C_H
#define C_H

#include "D.h"

Class C:
{
...
};
#endif 
Any chance there's something defined in two files? A function, a variable, anything?

[edit] I'm assuming D has header guards as well?
Last edited on
Its because of Transitive nature of inheritence! A is the subclass of B and C and classes B and C are subclasses of D. try placing a variable or anything like that, that can make A as a subclass of D. The problem is that A is not a subclass of D when it should be, causing the error!

check it out and reply. Hope I am not Wrong but I can be.

Also try this....

In the original program remove the class A. and then run the program and check whether it still gives the same error. if it doesnt A is making the chaos and if it does check the class declarations of all the classes and make sure that they all have at-least one common attribute starting from D to B and C to A!!

All the best!
D does have a header guard. The D header file has an enum and a class, and the errors say that I am redifining both the enum and the class, so I'm sure it's something to do with how I've included the header file twice.

absawis:
I'm not sure what you mean. The classes are not derived classes or anything like that (I don't know much about inheritance yet). I'll try explain what is going on better:

class A, there is some user input, based on this there are two possible outcomes
1)
still in A, a B object is created
within B, a D object is created
some functions are called from B and D
or 2)
still in A, a C object is created
within C, a D object is created
some functions are called from C and D

so A cannot work without both B and C, and B and C cannot work without D. Also B and C need to include D so they can create D objects, which is where my problem arises because it seems that is not allowed? hope that makes sense
Don't worry I've found the problem! Turns out I had missed off the _H in the #define part of the header guard for class D. I'm such a fool! I hate typos.

Thanks for trying to help anyway!
Topic archived. No new replies allowed.