2 classes holding instances of each other ( but it won't work :( )

G

Gizmo

hi all

i have a bit of a prob with some 2 classes iv created

if say i have class A and class B.

class A hold an atribute of class B and class be Holds an Atribute of class
A. both are declared as pointers in each others class and iv used #ifndef
in both the .h's of each class.

however c++ does not seem to like me doing this. . .is there any way around
this . . i can do it with one class holding an instance of the other but if
both try to have an instance of each other it throws out loads of errors.

a little example is below

thanks for any help
Giz

#ifndef CLASSA

#define CLASSA

#include "classB.h"



class classA

{

private:


int iSomthing;

classB *B;



public:

int getSomthing();

};



#endif


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

#ifndef CLASSB

#define CLASSB

#include "classA.h"

class classB

{

private :

int iAnotherSomthing;

classA *A;

public :

int GetAnotherSomthing();

};

#endif
 
P

Peter van Merkerk

i have a bit of a prob with some 2 classes iv created
if say i have class A and class B.

class A hold an atribute of class B and class be Holds an Atribute of class
A. both are declared as pointers in each others class and iv used #ifndef
in both the .h's of each class.

however c++ does not seem to like me doing this. . .is there any way around
this . . i can do it with one class holding an instance of the other but if
both try to have an instance of each other it throws out loads of
errors.

*example snippped *

Use forward declarations instead of #include:

// -----------classA.h------------
#ifndef CLASSA
#define CLASSA

class classB; // forward declaration

class A
{
private:
int iSomthing;
classB *B;
public:
int getSomthing();
};
#endif


// -----------classB.h------------
#ifndef CLASSB
#define CLASSB

class classA; // forward declaration

class classB
{
private :
int iAnotherSomthing;
classA *A;
public :
int GetAnotherSomthing();
};

#endif

The forward declaration tells the compile that classA and classB
identifiers represent classes. For pointer declarations that is all what
the compiler needs to know about the classA and classB identifiers; the
class definition itself is not needed in this case.
 
C

Christian Jaeger

class A hold an atribute of class B and class be Holds an Atribute ...

Did you mean instance here instead of at(t)ribute?

However, header A includes header B which in turn includes header A
which in turn includes header B wich ... is not a good idea. Indeed,
since you are storing pointers only, it is not necessary to include the
header A in B or B in A. All the compiler needs to know in header A is,
that there is a class classB, and class A hold a classB*. Change your
declarations to:

class classA{
int iSomthing;
class classB *B;
//^^^^^
public:
int getSomthing();
};
same for classB. Of course you need to include header B in the
implementation file of class A and vice versa, but this is no problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top