Jump to content
  • Advertisement
Sign in to follow this  
Mistro

Problems with Polymorphism

This topic is 4835 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

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(){}

	//Linked List stuff
	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)
	{
		m_pNext->AddItemAtEnd(eEntity);
		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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!