Hi
The g++ compiler generates the following error output for the code below. This code compiles fine in Visual Studio. Any ideas as to why this code fails in g++?
factory.h: In member function ‘bool Factory<Base>::Register(ObjectType, CreatorBase<Base>*)’:
factory.h:45: error: expected `;' before ‘it’
factory.h:47: error: ‘it’ was not declared in this scope
Thanks
Ken
factory.h
Code:
#ifndef FACTORY_
#define FACTORY_
#include <map>
#include <string>
typedef std::string ObjectType;
template<class Base>
class CreatorBase
{
public:
virtual ~CreatorBase() {}
virtual Base * Create() const = 0;
};
template<class Product, class Base>
class Creator : public CreatorBase<Base>
{
public:
virtual Base * Create() const {return new Product;}
};
template<class Base>
class Factory
{
public:
Base * Create(ObjectType type);
bool Register(ObjectType type, CreatorBase<Base> * pCreator) ;
private:
typedef std::map<ObjectType, CreatorBase<Base> *> CreatorMap;
CreatorMap m_creatorMap;
};
template<class Base>
bool Factory<Base>::Register(ObjectType type, CreatorBase<Base> * pCreator)
{
CreatorMap::iterator it = m_creatorMap.find(type);
if (it != m_creatorMap.end()) {
delete pCreator;
return false;
}
m_creatorMap[type] = pCreator;
return true;
}
template<class Base>
Base * Factory<Base>::Create(ObjectType type)
{
CreatorMap::iterator it = m_creatorMap.find(type);
if (it == m_creatorMap.end())
return NULL;
CreatorBase<Base> * pCreator = (*it).second;
return pCreator->Create();
}
#endif /*FACTORY_*/
entity.h
Code:
#ifndef ENTITY_H_
#define ENTITY_H_
class GameEntity
{
public:
virtual ~GameEntity() {}
virtual const char * GetClassName() { return "GameEntity"; }
};
class GameCamera : public GameEntity
{
public:
virtual const char * GetClassName() { return "GameCamera"; }
};
#endif /*ENTITY_H_*/
main.c++
Code:
#include <iostream>
#include "factory.h"
#include "entity.h"
int
main() {
std::cout << "Hello World" << std::endl;
Factory<GameEntity> factory;
factory.Register(GameCamera, new Creator<GameCameram,GameEntity>);
}