Jump to content
  • Advertisement
Sign in to follow this  
garyfletcher

Compliation problem with templates.

This topic is 4827 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 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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!