The problem you have is you have tried to inlcude ClassA.h into ClassB.h and ClassB.h into ClassA.h. This is impossible, 1 must be included before the other logically they can not both come first. In your case ClassB.h is the one that is included first followed by ClassA.h so the error is created in ClassB.h
This can be followed from the C file
C file includes ClassA.h
ClassA.h checks CLASSA_H it is not defined
ClassA.h defines CLASSA_H
ClassA.h includes ClassB.h
ClassB.h checks CLASSB_H it is not defined
ClassB.h defines CLASSB_H
ClassB.h includes ClassA.h
ClassA.h checks CLASSA_H it is defined, all code in ClassA.h is excluded
ClassB.h defines it's code, this code is dependent on class ClassA which the compiler has not see yet and an error is produced
There is an easy solution to this, you can forward declare the class, that is tell the compiler that there is going to be a class called ClassA without actually defining it. This works because class by only uses references to ClassA not any actual members for it so the compiler has all the information it requires to set-up ClassB (the fact that there will be a pointer to class in it).
If you do this then ClassB.h will look like
-
//File ClassB.h
-
#ifndef CLASSB_H
-
#define CLASSB_H
-
-
class ClassA;
-
-
class ClassB {
-
private:
-
ClassA *lnkBToA;
-
-
public:
-
ClassB(ClassA *lnk);
-
};
-
#endif
-
If the code of ClassB uses members of A through the pointer that is fine because you can include ClassA.h into ClassB.cpp, this just removes the dependency of the header files on each other.
You could also do the same thing for ClassA.h