Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

header file confusion

Posted on 1998-08-10
2
Medium Priority
?
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
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.
Suggested Courses

610 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