Jump to content

[Solved] C++ recursive including


AggrorJorn
 Share

Recommended Posts

Onto the next issue.smile.png I have two classes that both depend on each other. One of the classes even inherits from the other. I learned that I can use ifndef to prevent multiple including, but I am running into an issue: 'CComponent' : base class undefined

 

I am not sure why it says this since I am uncluding it. Can someone explain to me how this works?

 

GameObject

#ifndef GAMEOBJECT
#define GAMEOBJECT
#include "Leadwerks.h"
#include "CComponent.h"
using namespace Leadwerks;
class GameObject : public CComponent
{
public:
GameObject();
GameObject(GameObject* parent);

 

 

Component

#ifndef CCOMPONENT
#define CCOMPONENT
#include "GameObject.h"
class CComponent
{
public:
CComponent();
CComponent(GameObject* parent);

Link to comment
Share on other sites

You have to forward declare the other class inside the other class (not it's contents but just the name) in each. So after your include but before your class definition put:

 

class GameObject; // forward declaring the other class

class CComponent

{

..

 

 

class CComponent; // forward declaring the other class

class GameObject

{

...

 

 

When you do what you're doing you can also ONLY have pointers to the other class. This is because a pointer doesn't actually create the object at the header level so it doesn't need to know the class definition (because at that point the class definition is empty). By the time your constructor runs both official classes have been defined and then you can create the class object.

 

You have to do this so your pointer objects of the other type defined in the class, know that the class name is valid and defined at the header level and so it will then at least compile.

  • Upvote 1
Link to comment
Share on other sites

Forward declaration is the solution but as rick mentioned you can only use pointers.

 

You should avoid circular dependencies whereever possible.

 

 

// Edit:

 

You should also avoid "using namespace xxx;" in your header files, it can happen that you use another library and as soon as you include your header it will have multiple classes / types with the same name.

 

Use "using namespace xxx;" only in c++ files.

 

(of course your code will work but its bad style and you CAN run into problems later)

  • Upvote 1
Link to comment
Share on other sites

thanks for the help Rick.

 

I got rid of the error in the CComponent class, but the one in GameObject is still there. This is how the GameObject looks now:

 

#include "CComponent.h"
class CComponent;
class GameObject : public CComponent
{

 

 

@furbolg: This lies at the core for my own component based engine that runs on top of leadwerks. For its design it is important that they know about each other. But I agree that it is smarter to stay away from them.

Link to comment
Share on other sites

Error 2 error C2504: 'CComponent' : base class undefined ..\source\core\GameObject.h 11 1

 

 

CComponent


#ifndef CCOMPONENT
#define CCOMPONENT

#include "GameObject.h"

class GameObject;

class CComponent
{
public:
 CComponent();
 CComponent(GameObject* parent);
 ~CComponent();

 int ID;
 GameObject* parent;
 virtual void Init() = 0;
 virtual void Update() = 0;
 virtual void Draw() = 0 ;
 bool enabled;
};

#endif

 

GameObject


#ifndef GAMEOBJECT
#define GAMEOBJECT

#include "Leadwerks.h"
#include <list>
#include "CComponent.h"

class CComponent;

class GameObject : public CComponent
{
public:
 GameObject();
 GameObject(GameObject* parent);
 ~GameObject();

 void Init();
 void Update();
 void Draw();
 void RemoveComponent(CComponent* component);
 void AddComponent(CComponent* component);

 //holds all the components of that game object
 list<CComponent*> components;
};

#endif

Link to comment
Share on other sites

thanks for the help Rick.

 

I got rid of the error in the CComponent class, but the one in GameObject is still there. This is how the GameObject looks now:

 

#include "CComponent.h"
class CComponent;
class GameObject : public CComponent
{

 

You done it the wrong way, as rick mentioned you cant use forward declaration (btw. you are using forward declaration and the CComponent header file... this can't work) as a normal class (inherit from it) just as a pointer.

 

Your CGameObject has to include CComponent the normal way but your CComponent has to use forward declaration.

 

Forward declaration works like this:

//test.h:
class cgameobject;
class ccomponent
{
   public:
   ccomponent(cgameobject* obj);
   private:
   cgameobject* gameobject;
};

// test.c++
#include "test.h"
#include "cgameobject.h"
ccomponent::ccomponent(cgameobject* obj)
{
 this->gameobject = obj;
}

Link to comment
Share on other sites

Try this one:

 

CComponent


#ifndef CCOMPONENT
#define CCOMPONENT

class GameObject;

class CComponent
{
public:
CComponent();
CComponent(GameObject* parent);
~CComponent();

int ID;
GameObject* parent;
virtual void Init() = 0;
virtual void Update() = 0;
virtual void Draw() = 0 ;
bool enabled;
};

#endif

 

GameObject


#ifndef GAMEOBJECT
#define GAMEOBJECT

#include "Leadwerks.h"
#include <list>
#include "CComponent.h"

class GameObject : public CComponent
{
public:
GameObject();
GameObject(GameObject* parent);
~GameObject();

void Init();
void Update();
void Draw();
void RemoveComponent(CComponent* component);
void AddComponent(CComponent* component);

//holds all the components of that game object
list<CComponent*> components;
};

#endif

 

You have to add the GameObject Header include in the CComponent C++ File ( #include "gameobject.h" in ccomponent.cpp).

 

 

Sorry for double post :(

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...