Solved

header file confusion

Posted on 1998-08-10
2
168 Views
Last Modified: 2010-04-02
Hi,

  I want to create two classes, say class A and class B. For each class, I have a header file(A.h and B.h) as well as implementation file(A.cpp and B.cpp).  My class A has a member variable which is of the type class B. Also, in my class B, there is a member variable of type class A. i.e. the class definition looks like this:

class A {
B testMember;
...
};

class B {
A testMember;
...
};

I have "include A.h" in the B.h file and also, I "include B.h" in the A.h file. Initially i got error saying some kind of recursion definition too deep. I realized this error and added #ifdef and #endif to my program to avoid the recursion. But then I got some error saying missing decl-specifiers. I wonder what is the proper way of doing this? I am using MSVC 5.0.

Any help is appreciated!

sunjian
0
Comment
Question by:sunjian061197
2 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 200 total points
ID: 1169901
You can't have both A containing B and B containing A - consider the size of A, for example:
    sizeof(A) == sizeof(B) + size of the rest of A > sizeof(B)
But also:
    sizeof(B) == sizeof(A) + size of the rest of B > sizeof(A)

What you *can* do, is have A contain a pointer to B. So in A.h you should write:

class B; // forward declaration

class A
{
   B* testMember;
   ...
};

You don't need to #include B.h in A.h, only in A.cpp.

0
 

Author Comment

by:sunjian061197
ID: 1169902
Hi yonat,

   Thank you very much for your answer! You really saved me a lot of frustration...

sunjian
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now