Compliation problem with templates.

Started by
3 comments, last by Zahlman 18 years, 9 months ago
I have a template class and a non-member template function:

    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;
    }

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?
Gary.Goodbye, and thanks for all the fish.
Advertisement
vc6 has awful template support. now if i'm not mistaken...
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.
This space for rent.
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..:)
Gary.Goodbye, and thanks for all the fish.
like i said, far from expert with templates. at least i was close.
This space for rent.
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