Solved

Two classes seeing each other

Posted on 1998-01-07
9
176 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 165
AVI wait icons for CAnimateCtrl in Visual Studio 2008 MFC 1 119
C++ Language error 28 186
c++ getting the first 10 characters of a char* string 11 90
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

932 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

10 Experts available now in Live!

Get 1:1 Help Now