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;
typedef std::pair<ObjectType, CreatorBase<Base> *> CreatorPair;
CreatorMap m_creatorMap;
};
template<class Base>
bool Factory<Base>::Register(ObjectType type, CreatorBase<Base> * pCreator)
{
/* COMPILER ERROR HERE */
CreatorMap::iterator it = m_creatorMap.find(type);
if (it != m_creatorMap.end())
{
delete pCreator;
return false;
}
m_creatorMap.insert(pair(type,pCreator));
return true;
}
Compliation problem with templates.
I have a template class and a non-member template function:
I'm using DEV-C++ v4.9.9.2 and associated compiler (mingw, I think) and am getting the following error:
C:/SiSE/include/rm.h: In member function `bool rm::Factory<Base>::Register(ObjectType, rm::CreatorBase<Base>*)':
C:/SiSE/include/rm.h:46: error: expected `;' before "it"
The original code was done in VC++ 6, and I'm wondering if the problem is to do with the way that the compilers deal with templates OR if I'm doing something (or not doing something) I shouldn't.
Does anyone have any ideas?
vc6 has awful template support. now if i'm not mistaken...
should be
but i'm far from an expert on templates. my apologies if i'm wrong.
bool Factory<Base>::Register(ObjectType type, CreatorBase<Base> * pCreator)
should be
bool Factory<Base>::Register(ObjectType type, typename CreatorBase<Base> * pCreator)
but i'm far from an expert on templates. my apologies if i'm wrong.
Bloody hell, of course.
You were almost right grumpy.
Actually needed a typename when I declare the iterator:
typename CreatorMap::iterator it = m_creatorMap.find(type);
Thanks for taking a look..:)
You were almost right grumpy.
Actually needed a typename when I declare the iterator:
typename CreatorMap::iterator it = m_creatorMap.find(type);
Thanks for taking a look..:)
The reason is that "CreatorMap::iterator" looks like it could refer to a static data member in the CreatorMap class just as easily as it could refer to a typedef in that class; the 'typename' keyword is required to disambiguate this. (The compiler can normally figure it out from context anyway, but the standard says it's not required to do so.)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement