Solved

Two classes seeing each other

Posted on 1998-01-07
9
165 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Post code if you need more help.
0
 

Author Comment

by:asquarius
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 7

Expert Comment

by:galkin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

744 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

17 Experts available now in Live!

Get 1:1 Help Now