Solved

Two classes seeing each other

Posted on 1998-01-07
9
186 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
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

810 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