Jump to content
  • Advertisement
Sign in to follow this  
Roziks_World

include problem

This topic is 3599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!