Sign in to follow this  

Having trouble organizing my classes

This topic is 3847 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. Warning: Long post. :) Recently it seems my code design has become a bit too messy because of my lack of ability to organize it all properly. :P As a result, headers include other headers, who then includes the FIRST headers and so on. This causes a few problems obviously. First I thought I found a solution by declaring the classes in the headers after the include, but before they are used. It worked mostly. But not perfectly. I'll explain my whole structure of classes. I got a bunch of resource managers, which are all derived from one base class. Their job is to load game data from files so I can access the data whenever I want to for instance make a new object with a specific sprite. The texture manager, sprite manager and tile set manager are all residing in the engine part, in a DLL. Also I got a gamestate class, which can handle the different "parts" of the game (main menu, in-game, high-scores, whatever I need). I can derive it in the game part of the game to make for instance a main menu. I also got a base CGame class. It's job is to hold everything together, manage game states, and contain the resource managers. Both the game class and the game state class resides in the engine DLL file. All is well so far. Even though the gamestate.h-file needs to include game.h, and game.h needs to include gamestate.h, that is easily solved by declaring the classes before they are used, after the headers are included like this: gamestate.h: #include "game.h" class CGame; class CGameState {...}; game.h: #include "gamestate.h" class CGameState; class CGame {...} Ok, moving on. The problem starts in the game part of my game. In the game part, I made another resource manager to hold loaded game objects. I called it CObjectManager, and the header includes the file gameobject.h. I keep this object manager in the derived version of CGame, let's call it CThisGame. I also got a environment class called CEnv, which contains all the objects currently in the "environment". In other words, it's the "playfield" - all objects and the map, which will handle physics, movement and everything. The env.h header needs to include the 1. thisgame.h so it can access the object database (a pointer to an instance of the game class is passed to all functions that needs it), and 2. gameobject.h (obviously) since the CEnv class will handle all objects. The thisgame.h file needs to include "objectmanager.h". "objectmanager.h" includes the "gameobject.h" file. "gameobject.h" includes the "env.h" file, which then includes the thisgame.h file (and gameobject.h file). I thought I could fix it all by declaring all classes that are used before I use them, but I couldn't. I get a nasty "error C2079: 'CThisGame::m_ObjectManager' uses undefined class 'CObjectManager'" when compiling thisgame.h. I understand the error message, but I have no idea how I can organize my code/classes so my code won't get so messy, and in turn fix that error. A little summary: thisgame.h: Includes objectmanager.h. objectmanager.h: Includes gameobject.h. gameobject.h: Includes env.h. env.h: Includes gameobject.h AND thisgame.h. The line that creates the error is: class CThisGame : public CGame { private: CObjectManager m_ObjectManager; <---- The guilty line but I suspect my code needs more than fixing JUST this line. I think it needs serious reorganizing. :P So my question is then really: How can I organize my classes so this problem won't occur? How can I organize them so I from the game object class can access both the object/sprite database and environment class, with minimal "header includes header, which includes first header"-issues? I'm not a very experienced game programmer as you probably understand by now, as I'm only doing it on a hobby basis, so I want to ask here where there ARE experienced game programmers. :) Goddamn, sorry for the huge post, but my code is messy even for me so wanted to explain it properly. I hope I succeeded in THAT at least. If not, let me know so I can clarify. Thanks for your time, and all help is greatly appreciated.

Share this post


Link to post
Share on other sites
I don't have time to suggest you a better structure (this is not something you can just think of - it requires a pen, a notebook, etc), but I think I can help you with the technical details.

You only need to declare a class (lets call it CUsedClass) used by another class (lets call it CUserClass) before the CUserClass when CUserClass declares a variable of CUsedClass. When CUserClass declares a *reference* or a *pointer* to a variable of CUsedClass, the header *does not have to be included*, a single-line declaration is enough (class CUsedClass;). Then in cpp file for CUserClass, you actually include the h file for CUsedClass. Also, if CUserClass was derived from CUsedClass, then you would have to include CUsedClass's h file in the CUserClass's h file.

Let's put this in code:

Scenario 1 - declaring a variable (not a pointer or a reference)

//////////////////////////////////
// UsedClass.h
//////////////////////////////////

#pragma once

class CUsedClass
{
public:
CUsedClass();
~CUsedClass();

// Other members follow...
};

//////////////////////////////////
// UsedClass.cpp
//////////////////////////////////

#include "UsedClass.h"

CUsedClass::CUsedClass()
{
}

CUsedClass::~CUsedClass()
{
}

// Other member's definitions follow

//////////////////////////////////
// UserClass.h
//////////////////////////////////

#pragma once

// We are using a variable of CUsedClass, so we must include its header
#include "UsedClass.h"

class CUserClass
{
private:
CUsedClass m_usedClass;

public:
CUserClass();
~CUserClass();

// Other members follow
};

//////////////////////////////////
// UserClass.cpp
//////////////////////////////////

#include "UserClass.h"

CUserClass::CUserClass()
{
}

CUserClass::~CUserClass()
{
}

// Other member definitions follow, that do something to CUsedClass




It would be the same thing if CUserClass was derived from CUsedClass.


Scenario 2 - declaring a pointer to a variable


// UsedClass is unchanged

//////////////////////////////////
// UsedClass.h
//////////////////////////////////

#pragma once

class CUsedClass
{
public:
CUsedClass();
~CUsedClass();

// Other members follow...
};

//////////////////////////////////
// UsedClass.cpp
//////////////////////////////////

#include "UsedClass.h"

CUsedClass::CUsedClass()
{
}

CUsedClass::~CUsedClass()
{
}

// Other member's definitions follow

//////////////////////////////////
// UserClass.h
//////////////////////////////////

#pragma once

// We are not including UsedClass.h, only declaring it

class CUsedClass;

class CUserClass
{
private:
CUsedClass* m_pUsedClass; // pointer!

public:
CUserClass();
~CUserClass();

// Other members follow
};

//////////////////////////////////
// UserClass.cpp
//////////////////////////////////

#include "UsedClass.h" // include UsedClass.h before the UserClass.h
#include "UserClass.h"

CUserClass::CUserClass(): m_pUsedClass(new CUsedClass)
{
}

CUserClass::~CUserClass()
{
}

// Other member definitions follow, that do something to CUsedClass




I hope I didn't misread your post and that this will help you.

[Edited by - ValMan on June 3, 2007 1:41:14 PM]

Share this post


Link to post
Share on other sites
Hi, and thanks for replying. :)

So you say that I could use a pointer declaration instead of a normal variable declaration to get around that problem, without even including the header? I love it.
I temporarily fixed the problem by moving my object manager to the game state class that needs it - but it's not a long term solution obviously. I'm definitely going for the solution you suggested now.

Thanks a lot for your help! :)

(Ps: Comments/suggestions on my class structure is of course still welcome... but I think if this works, my actual problem is solved. Thanks again. :))

Share this post


Link to post
Share on other sites

This topic is 3847 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.

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