Sign in to follow this  
garyfletcher

Compliation problem with templates.

Recommended Posts

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?

Share this post


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

Share this post


Link to post
Share on other sites
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..:)

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites

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