471,850 Members | 1,074 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,850 software developers and data experts.

bi-directional relationship include problems

Helo everybody!

I need help with a error I get compiling the folowing code:
//File ClassA.h
#ifndef CLASSA_H
#define CLASSA_H
#include "ClassB.h"

class ClassA{
ClassB *lnkAToB;
ClassA(ClassB *lnk);
//File ClassA.cpp
#include "ClassA.h"

ClassA::ClassA(ClassB *lnk){
this->lnkAToB = lnk;

//File ClassB.h
#ifndef CLASSB_H
#define CLASSB_H
#include "ClassA.h"
class ClassB {
ClassA *lnkBToA;
ClassB(ClassA *lnk);
//File ClassB.cpp
#iclude "ClassB.h"

ClassB::ClassB(ClassA *lnk){
this->lnkBToA = lnk;

the error I get compiling is:
"ClassB.h": E2303 Type name expected at line 6
Sep 17 '06 #1
2 4239
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. //File ClassB.h
  2. #ifndef CLASSB_H
  3. #define CLASSB_H
  5. class ClassA;
  7. class ClassB {
  8. private:
  9.   ClassA *lnkBToA;
  11. public:
  12.   ClassB(ClassA *lnk);
  13. };
  14. #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
Sep 17 '06 #2
Thank you very much you saved my live!!!
Very nice explanation!
Sep 17 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by V?ronique | last post: by
3 posts views Thread by ShihChengYu | last post: by
15 posts views Thread by rinmanb70 | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.