Solved

header file confusion

Posted on 1998-08-10
2
167 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

13 Experts available now in Live!

Get 1:1 Help Now