Sign in to follow this  
Roziks_World

include problem

Recommended Posts

Hi everyone, i am having a little problem with includes. I have two classes and i want each class to have a reference to the other but since the once class is already defined the include in the second class doesnt work at all :-( here is an example code: Class A:
#ifndef CLASSA_H
#define CLASSA_H
#include "Class_B.h"
class A_class{
public:
      B_class ref;  
}
#endif
Class B:
#ifndef CLASSB_H
#define CLASSB_H
#include "Class_A.h"
class B_class{
public:
      A_class ref;  
}
#endif
when the second is compiling it says A_Class isnt defined. Can anyone please tell me how to solve this? Thx a lot in advance!

Share this post


Link to post
Share on other sites
Quite simply this can't be done. It is a circular dependency and should be avoided.

You have some choices though:

-redesign around the circular dependency (recommended, a circular dependency is indicative of a design flaw)

-use pointers:

ClassA.h



#ifndef CLASSA_H

#define CLASSA_H

// forward declaration
class B_class;

class A_class{

public:

B_class* ref;

}

#endif




ClassB.h



#ifndef CLASSB_H

#define CLASSB_H

// forward declaration
class A_class;

class B_class{

public:

A_class* ref;

}

#endif




Then include class A's header in B's source file, and vice-versa.

Share this post


Link to post
Share on other sites
Quote:
Original post by Roziks_World
Hi everyone,

i am having a little problem with includes.

I have two classes and i want each class to have a reference to the other but since the once class is already defined the include in the second class doesnt work at all :-(

here is an example code:

Class A:

#ifndef CLASSA_H
#define CLASSA_H
#include "Class_B.h"
class A_class{
public:
B_class ref;
}
#endif


Class B:

#ifndef CLASSB_H
#define CLASSB_H
#include "Class_A.h"
class B_class{
public:
A_class ref;
}
#endif


when the second is compiling it says A_Class isnt defined.

Can anyone please tell me how to solve this?

Thx a lot in advance!
You can't do that - Class A includes class B which inclues class A which includes class B, etc. You'll get an infinitely sized class.

You'll have to store a reference or pointer to class A in class B (or vice-versa), and then use a forward declaration in Class_B.h. Example:

// Class_A.h
#ifndef CLASSA_H
#define CLASSA_H
#include "Class_B.h"
class A_class{
public:
B_class ref;
}
#endif

// Class_B.h
#ifndef CLASSB_H
#define CLASSB_H
class A_class; // Forward declare A_class
class B_class{
public:
A_class* ref;
}
#endif

That forward declaration tells the compiler that A_class exists, and that it's a class. You can then #include "Class_A.h" in Class_B.cpp.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this