473,473 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

bi-directional relationship include problems

22 New Member
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{
private:
ClassB *lnkAToB;
public:
ClassA(ClassB *lnk);
};
#endif
//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 {
private:
ClassA *lnkBToA;
public:
ClassB(ClassA *lnk);
};
#endif
//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 4462
Banfa
9,065 Recognized Expert Moderator Expert
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
  4.  
  5. class ClassA;
  6.  
  7. class ClassB {
  8. private:
  9.   ClassA *lnkBToA;
  10.  
  11. public:
  12.   ClassB(ClassA *lnk);
  13. };
  14. #endif
  15.  
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
nomad5000
22 New Member
Thank you very much you saved my live!!!
Very nice explanation!
Sep 17 '06 #3

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

Similar topics

0
by: DD | last post by:
Hi Guys! Just would like to share with you my experiense in this matter. I was trying to evaluate how suitable Oracle OLAP for our applications. As probably you did, I have downloaded from OTN...
0
by: V?ronique | last post by:
hi everyone, if you are a BI tool user, speak spanish, then you might be interested in decideo, which is a community of BI tool users. the newsletter is on: ...
0
by: YellowFin Announcements | last post by:
Announcing Yellowfin Version 2.4 release. (www.yellowfin.com.au) Existing SQLserver users can take advantage of this upgrade as soon as convenient. V2.4 Exciting changes in the latest...
3
by: ShihChengYu | last post by:
Dear all: How to convert color image to bi-level image? I have confronted one problem when I build my OCR project. I used an software API function to enhance my project, but the API only...
1
by: jimdandy | last post by:
Hi all, Am looking for some guidance/advice in terms of best practices for deploying a MS BI project. We have a relatively large BI system that we need to deploy between DEV/QA/UAT/PROD and...
0
by: YellowFin | last post by:
Yellowfin International today announced that Yellowfin has joined IBM's Partner Program, and that the Yellowfin BI Suite are now validated on IBM's popular DB2 9 database. This partnership opens...
0
by: WI Projects | last post by:
Hi, A CMM Level 5 company is looking for Microsoft Business Intelligence (MS BI) professionals for its Hyderabad location. There is a an element of onsite travel & work to this position. ...
15
by: rinmanb70 | last post by:
I use a db for keeping up with my checkbook and I'm having trouble setting up a new functionality. I get paid every two weeks on Friday, and I need a report to figure my balance that includes only...
1
by: creative1 | last post by:
Hi Everyone, I need help in one more thing. I my report that I want to print under follwoing criteria: Weekly ---------------- Set start and end date automatically to last week Bi-Weekly ...
0
by: zyro | last post by:
Hi, This is Michael Bowen. I've put together cubegeek.com as a new and hopefully exciting site for the BI professional to network, share get information. This is the industry's first portal...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.