Archived

This topic is now archived and is closed to further replies.

Bullmax

Trouble with classes includes !

Recommended Posts

Hello, I'm quite experienced with game programming, but my problem is a newbie one... I have troubles with includes. I have various classes for like : - CCast - CMember - CScene - CSprite - CScript CCast : Library of medias Does not have inheritance, but have CMember** (list of childs) type inside it. CMember : Any media Does not have inheritance, but have CCast* (parent) type inside it. CScene : Scene with objects Does not have inheritance, but have CSprite** (list of childs) type inside it CSprite : Instance of bitmap member Inherits from CScript (because it is also a script) and have CMember* (media reference) type inside it. CScript : Linked nodes of execution Does not have inheritance, but have CScript** (list of childs) and CScript* (parent) type inside it. To declare safely, I define my classes before use in a file named "Classes.h" like this : class CCast; class CMember; class CScene; class CSprite; class CScript; Then it is followed by includes of their header containing all the complete class declaration like this : #include "CCast.h" #include "CMember.h" #include "CScene.h" #include "CSprite.h" #include "CScript.h" In each header I have the complete class declaration. The problem is with "CSprite" when I do inheritance like this class CSprite : public CScript { public: CScript(); ~CScript(); Render(); public: CMember *m_pMember; POINT m_ptPosition; FLOAT m_fRotation; POINT m_ptSize; INT m_intZ; DWORD m_dwColoration; }; The compiler gives me an error in the declaration of the class "class CSprite : public CScript" telling me that it has not been declared ! but it is, no ? Also, the compiler does not give me an error for my "CMember *m_pMember" class member ! it is I, or there is a lack of logic here... thanks in advance
/* Bullmax */ ------------- Reality has many forms : good and evil, black and white, ying and yang. [edited by - Bullmax on November 1, 2002 9:38:30 PM]

Share this post


Link to post
Share on other sites
To inherit from a class, the complete declaration of that class has to be "visible" (or whatever the technical term should be).

Thus, the following code will fail:


class MyParentClass;

class MyChildClass : public MyParentClass
{
private:
int someData;

public:
void someFunction();
};


The reason this code fails is because MyChildClass is inheriting from the incomplete type MyParentClass. The following code corrects this:


class MyParentClass
{
public:
void someParentFunction();
};


class MyChildClass : public MyParentClass
{
private:
int someData;

public:
void someFunction();
};


This works because MyParentClass is now complete.

As com alluded to, in your example the declaration of CScript (the parent class) is included after the declaration of CSprite (the child class). These includes must be reversed, so that:


#include "CCast.h"
#include "CMember.h"
#include "CScene.h"
#include "CSprite.h"
#include "CScript.h"


becomes:


#include "CCast.h"
#include "CMember.h"
#include "CScene.h"
#include "CScript.h"
#include "CSprite.h"


The reason that "CMember *m_pMember;" does not give an error is that the compiler knows that no matter what exactly a CMember is, a pointer to a CMember will always be a four byte entity. So when declaring pointers to classes, an incomplete forward declaration such as "class CMember;" is enough. If the variable you were declaring was not a pointer, however, the compiler would have complained about it. Thus, the following code would give an error:



class MyClass;

class MyClassUser
{
private:
MyClass* m_pMyClass; // this line is OK -- pointer so no error
MyClass m_MyClass; // this line gives an error
};


Sorry that was so long...hopefully it makes sense. If you have any other questions, just ask.

Share this post


Link to post
Share on other sites