Public Group

# Problems with Polymorphism

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

## Recommended Posts

Well heres a basic description of the problem. I have a base class called CEntity, from which I wish to derive some other classes on.
class CEntity
{
public:
CEntity();
virtual ~CEntity();

virtual void Init(){}
virtual void Update(float fET){}
virtual void Release(){}

CEntity* Next() { return m_pNext; }		//Gets Next Node
CEntity* Prev() { return m_pPrev; }		//Gets Prev Node

........


Now just to test if this thing works, I then created a child class called CEntityBlank.
#include "CEntity.h"

class CEntityBlank: public CEntity
{
public:
CEntityBlank() {}
~CEntityBlank() {}

void Init() {}
void Update(float fET) {}
void Release() {}

private:
//nothing...
};

Now I have this Manager class which has this :
#include "CEntity.h"
#include "CEntityBlank.h"

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

...

private:
CEntity* m_pRoot;
...


Now in my Source file I have:
CEntityManager::CEntityManager()
{
m_pRoot = new CEntityBlank();
}

Now if I have done this correctly, this should be working... However, I get this compile error: error C2061: syntax error : identifier 'CEntityBlank' Which points to the line saying m_pRoot = new CEntityBlank(); Now this is really ticking me off because I seem to have tried all possible solutions to it. If I comment out that line, the program compiles no problem. However, I do need that line :) This also appears in the only other place as of now where Im creating a new object...
void CEntity::AddEmptyItemAtEnd(MDENTITY eEntity)
{
if (m_pNext)
{
return;
}

switch (eEntity)
{
case ENT_BLANK:
{
m_pNext = new CEntityBlank();  /// -> error C2061: syntax error : identifier 'CEntityBlank' Comes up

}break;
// ETC...
}

m_pNext->m_pPrev = this;

}

Any ideas as to why its happening?

##### Share on other sites
It sounds like you might be having trouble with circular includes. This will help you.

##### Share on other sites
After reading that article I think that that may be the problem which I am having but I am unsure as to where it is.

Here is a basic structure for the three parts:

http://www.ezilnila.com/thivyan/progflow.JPG

And here is a breakdown of it:

///CEntityManager.h
#include "CEntity.h"
#include "CEntityBlank.h"

class CEntityManager
{
...

///CEntity.h
//No includes

class CEntity; //forward declaration for the linked list

class CEntity
{
...

///CEntityBlank.h
#include "CEntity.h"

class CEntityBlank: public CEntity
{
...

Now the only problem where I see a problem (?) is because the manager is including the EntityBlank and the Entity, whele the EntityBlank is also including the Entity which may cause a problem?

As far as I can see, they are all one way dependancies...

If I am missing something blatently stupid please feel free to point it out :)

EDIT: I guess i should also include that in all my header files I have put the #ifnotdef/#define preprocessor statments. (IE: #ifndef CENTITY_H #define CENTITY_H)

##### Share on other sites
Actually, from that it looks like circular includes aren't your problem. Drat.

##### Share on other sites
Ok after changing all the Headers to have #pragma once Instead of the #ifndef, It seems to be working quite well. No clue why that is... But it semifixed now :) However, it seems that I have indeed fallen into the 1% of times when solution 2 will not work [depressed]

Inside the CEntity functions I have this:

void CEntity::AddItemAtEnd(MDENTITY eEntity){...m_pNext = new CEntityBlank();}

Now this means that I will have to include CGameEntityBlank.h. However, if I do this I introduce problem 2 into it as this is the header for that:
///CEntityBlank.h
#include "CEntity.h"

The thing is however, the solution for problem 2 deals only with if the parent class only needs a pointer to the child class.

In this situation this is not the case since a new instance of the class needs to be made and that requires knowledge of the constructor.

So this is where the problem stands since the article dosn't cover the other 1% of times where that solution will not work...

##### Share on other sites
Once you get to the source files, there's no problems with circular includes. Include just as many header files in a source file as you like.

##### Share on other sites
Oh my goodness! Thank you so much! I feel totaly dumb that I forgot that other header files can be added in the source files [embarrass]

So in the CEntity source file I just have this and problem solved!
#include "CEntity.h"#include "CEntityBlank.h"

Thanks once again

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 9
• 25
• 54