AggrorJorn Posted April 27, 2013 Share Posted April 27, 2013 Onto the next issue. 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); Quote Link to comment Share on other sites More sharing options...
Rick Posted April 27, 2013 Share Posted April 27, 2013 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. 1 Quote Link to comment Share on other sites More sharing options...
Furbolg Posted April 27, 2013 Share Posted April 27, 2013 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) 1 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted April 27, 2013 Author Share Posted April 27, 2013 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. Quote Link to comment Share on other sites More sharing options...
Rick Posted April 27, 2013 Share Posted April 27, 2013 What's the exact error and if you could post the entire class definition of GameObject please. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted April 27, 2013 Author Share Posted April 27, 2013 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 Quote Link to comment Share on other sites More sharing options...
Furbolg Posted April 27, 2013 Share Posted April 27, 2013 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; } Quote Link to comment Share on other sites More sharing options...
Furbolg Posted April 27, 2013 Share Posted April 27, 2013 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 1 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted April 27, 2013 Author Share Posted April 27, 2013 Ahh superb. Its working now. The errors I get now are not related to these classes, so that is good news. I guess I am spoiled from using C# and Java. Thanks you both. this has been very helpful. Quote Link to comment Share on other sites More sharing options...
Furbolg Posted April 27, 2013 Share Posted April 27, 2013 No problem. Be careful, that your components dont lead you into "The diamond problem" http://en.wikipedia.org/wiki/Multiple_inheritance Quote Link to comment Share on other sites More sharing options...
xtreampb Posted April 27, 2013 Share Posted April 27, 2013 which is why all base class functions should be declared virtual Quote bool Life() { while(death=false) { if(death==true) return death; } } I have found the secret to infinite life Did I help you out? Like my post! Link to comment Share on other sites More sharing options...
Recommended Posts
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.