Solved

header file confusion

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 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 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.

856 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