?
Solved

Two classes seeing each other

Posted on 1998-01-07
9
Medium Priority
?
209 Views
Last Modified: 2010-07-27
I have two classes, ClassA and ClassB.

ClassA needs a global variable of ClassB and ClassB needs one of ClassA.

Therefore the header file for ClassA needs to be included in ClassB's header file and ClassB's header file needs to be included in ClassA's.

However the compiler does not like this.

I get the compiler error "'ClassA' : missing decl-specifiers" when I try to declare ClassA in ClassB.

can anyone help?
0
Comment
Question by:asquarius
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 400 total points
ID: 1177687
I don't understand why you need to include one header into another and opposite.Header file is not compiled only .cpp or .c. Also global variable can be declared only in one of .cpp files og the project. So to avoid compiler errors you must be sure you included in both classA and classB header files in .cpp file where global variables are defined.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177688
Do you mean global variable?  or static variable?  A class can not have a global variable.  Only a source code file (officially a translation unit) can have a global variable.  A class can have a static variable (member) which is similar.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177689
Post code if you need more help.
0
Independent Software Vendors: 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!

 

Author Comment

by:asquarius
ID: 1177690
OK, what I mean is this:

In header file for ClassA:

#include "ClassB.h"

class ClassA
{
   private:
   ClassB   b;    //private global variable for classA of classB
}

and in header of classB

#include "ClassA.h"

class ClassB
{
   private:
   ClassA   a;    //private global variable for classB of classA
}

I am have actually derived class a from CSocket in MFC.
my socket class is instantiated in class B as a global variable to B.  When the OnReceive handler is called, I want to call back to class B with the data I have read in, having previously assigned a pointer of classB in my socket class to point to my classB.

Does that make more sense?

thanks a lot

0
 
LVL 7

Expert Comment

by:galkin
ID: 1177691
Variable b of type classB declared in class classA and a of type classA declared in classB are not global variables! Global variable cannot be class member. If you declare ClassA a; or ClassB b; in any .cpp or .c files so you declare global variables and each of these variables belongs to entire project and therefore can be acessed by any class or function or other global variable in your project.

In your case to properly compile files you must do the following:

// file ClassA.h

class classB;

class ClassA
{
private:
ClassB b; //private global variable for classA of classB
}

/////////////////////////////

// file ClassB.h

class classA;

class ClassB
{
private:
ClassA a; //private global variable for classB of classA
}

//////////////////////////////////////////////////

// file ClassA.cpp

#include "ClassA.h"
#include "ClassB.h"

//////////////////////////////////////////////////

// file ClassB.cpp

#include "ClassA.h"
#include "ClassB.h"



0
 
LVL 22

Expert Comment

by:nietod
ID: 1177692
Does that make sense?  no.

Im confussed.  (And so are you, I think)

First of all, the following design (slightly simplied for space savings) is impossible.

class ClassA
{
   ClassB b;
}

class ClassB
{
   ClassA a;
}

What this "says" is that a ClassA contains within it a ClassB.  However a ClassB contains in it a ClassA (which in turn contains a ClassB, which contains an A wich contains a B etc.)  This classes would have to be infinitly large to contain an infinite number of copies of themselves within themselves.  This is a problem.  Why would you want this?

I can't understand your socket explanation.  (A little more detail/example might help).  However, the most likely fiis is that one class does not need to contain the other, but only needs  a pointer to the other.  Actually it is possible that both classes only need pointers to the other.

Something like

class ClassA
{
   ClassB *bptr;
}

class ClassB
{
   ClassA *aptr;
}

Another point of confussion is that

class ClassA
{
private:
   ClassB b; //private global variable for classA of classB
}
b is not a global variable.  it is a member of A.  Do you understand the difference?  Perhaps that is the confussion?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177693
I see that galkin was commenting while I was.  He cleaned up your code some, however, he seems to have missed the fact that each class includes the other.  As I said, this is not allowed.
0
 
LVL 7

Expert Comment

by:galkin
ID: 1177694
Yes you are right this is not allowed. I must have cautch this from the beginning. Compiler cannot create code for allocating memory for class classA since it includes classB variable which in turn depends on class classA. Only pointers are allowed
0
 

Author Comment

by:asquarius
ID: 1177695
OK, I have got it to work with both your help.  What I wanted (which is also what you guessed I wanted but I didn't explain very well) was, drum roll please...

--ClassA.cpp file

#include "ClassA.h"
#include "ClassB.h"

--ClassA.h file

class ClassB;

class ClassA
{
   ClassB  *b    //a pointer which is a member variable
}

--ClassB.cpp file

#include "ClassA.h"
#include "ClassB.h"

--ClassB.h file

class ClassA;

class ClassB
{
   ClassB  *b    //a pointer which is a member variable
}

My misunderstandings and confusions were:

Not declaring a pointer to the class, declaring an instance instead (Duh).

Using the keyword global where I trully meant member.  When I said global variable I meant it was global to the class which nietod pointer out was actually refered to as a member variable.

For my shortcomings I apologise for my triumphs I celebrate.

Not wanting to cause any bad feeling after the help you have both given, I would like to give you both the grade that I feel it is worth.  I am going to enquire with experts exchange on how I can do this.

Thanks a lot
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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

764 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