Sign in to follow this  

Overloading

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

I'm trying to implement some XML code to setup my scene in a game, although having some difficulties with function overloading. Below is mycode to explain: EntityManager.h
CEntity* CEntityManager::CreateEntity( const string& type )
{
	// Create new entity using extensible object factory
	CEntity* newEntity = m_EntityFactory.Create( type );
	newEntity->SetUID( m_NextUID );

	// Get vector index for new entity and add it to vector
	TUInt32 entityIndex = static_cast<TUInt32>(m_Entities.size());
	m_Entities.push_back( newEntity );

	// Add mapping from UID to entity index into hash map
	m_EntityUIDMap->SetKeyValue( m_NextUID, entityIndex );
	
	// Increase UID ready for next entity
	++m_NextUID;

	m_IsEnumerating = false; // Cancel any entity enumeration (entity list has changed)
	return newEntity;
}


// Create a base class entity - requires a template name, may supply entity name and position
// Returns the UID of the new entity
TEntityUID CEntityManager::CreateEntity
(
	const string&    templateName,
	const string&    name /*= ""*/,
	const CVector3&  position /*= CVector3::kOrigin*/, 
	const CVector3&  rotation /*= CVector3( 0.0f, 0.0f, 0.0f )*/,
	const CVector3&  scale /*= CVector3( 1.0f, 1.0f, 1.0f )*/
)
{
	// Get template associated with the template name
	CEntityTemplate* entityTemplate = GetTemplate( templateName );

	// Create new entity with next UID
	CEntity* newEntity = new CEntity( entityTemplate, m_NextUID, name, position, rotation, scale );

	// Get vector index for new entity and add it to vector
	TUInt32 entityIndex = static_cast<TUInt32>(m_Entities.size());
	m_Entities.push_back( newEntity );

	// Add mapping from UID to entity index into hash map
	m_EntityUIDMap->SetKeyValue( m_NextUID, entityIndex );
	
	m_IsEnumerating = false; // Cancel any entity enumeration (entity list has changed)

	// UID of new entity then increase it ready for next entity
	return m_NextUID++;
}

CParseLevel.cpp
// Create an entity using data collected from parsed XML elements
void CParseLevel::CreateEntity()
{
	// Get the template and template type that this entity uses
	CEntityTemplate* entityTemplate = m_EntityManager->GetTemplate( m_EntityType );
	string templateType = entityTemplate->GetType();

	// Create a new entity of this template type
	CEntity* newEntity = m_EntityManager->CreateEntity( templateType );
	if ( templateType == "Tank")
	{
		CTankEntity* tankEntity = reinterpret_cast<CTankEntity*>(newEntity);
		CTankTemplate* tankTemplate = reinterpret_cast<CTankTemplate*>(entityTemplate);
	}
}

As you can see in the CParseLevel, I want to return a CEntity* not a TEntityUID, but for some reason the compiler can't decide between the two. Anyone know of any ideas ?

Share this post


Link to post
Share on other sites
Yeh, the compiler errors are as follows:


1>c:\users\owner\desktop\final tank\tankassignment\source\data\cparselevel.cpp(239) : error C2668: 'gen::CEntityManager::CreateEntity' : ambiguous call to overloaded function
1> c:\users\owner\desktop\final tank\tankassignment\source\scene\entitymanager.h(113): could be 'gen::CEntity *gen::CEntityManager::CreateEntity(const std::string &)'
1> c:\users\owner\desktop\final tank\tankassignment\source\scene\entitymanager.h(81): or 'gen::TEntityUID gen::CEntityManager::CreateEntity(const std::string &,const std::string &,const gen::CVector3 &,const gen::CVector3 &,const gen::CVector3 &)

Share this post


Link to post
Share on other sites
Thats because you use implicit parameters for second function and overloading does not work for return parameters only.

For "m_EntityManager->CreateEntity( templateType )"

compiler has two *equal* choices:

CEntity* CEntityManager::CreateEntity( const string& type )

or

TEntityUID CEntityManager::CreateEntity( const string& type )

Share this post


Link to post
Share on other sites

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