By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

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

P: n/a
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



Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> 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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #2

P: n/a

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.

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.