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

x
?
Solved

Two classes seeing each other

Posted on 1998-01-07
9
Medium Priority
?
212 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

618 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