Thread: Compile issue in g++, works fine in VS

  1. #1
    Registered User
    Join Date
    Aug 2007

    Compile issue in g++, works fine in VS

    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


     #ifndef FACTORY_
    #define FACTORY_
    #include <map>
    #include <string>
    typedef std::string ObjectType;
    template<class Base>
    class CreatorBase
        virtual ~CreatorBase() {}
        virtual Base * Create() const = 0;
    template<class Product, class Base>
    class Creator : public CreatorBase<Base>
        virtual Base * Create() const {return new Product;}
    template<class Base>
    class Factory
        Base * Create(ObjectType type);
        bool Register(ObjectType type, CreatorBase<Base> * pCreator) ;
    	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_*/
    #ifndef ENTITY_H_
    #define ENTITY_H_
    class GameEntity
        virtual ~GameEntity() {}
        virtual const char * GetClassName() { return "GameEntity"; }
    class GameCamera : public GameEntity
        virtual const char * GetClassName() { return "GameCamera"; }
    #endif /*ENTITY_H_*/
    #include <iostream>
    #include "factory.h"
    #include "entity.h"
    main() {
    std::cout << "Hello World" << std::endl;
    Factory<GameEntity> factory;
    factory.Register(GameCamera, new Creator<GameCameram,GameEntity>);
    Last edited by bean66; 04-17-2009 at 02:27 PM.

  2. #2
    The larch
    Join Date
    May 2006
    That should be a rather standard issue. What CreatorMap::iterator is depends on the template type arguments, and in such cases in standard C++ it is required to disambiguate whether it is or is not a typename by using the typename keyword:

        typename CreatorMap::iterator  it = m_creatorMap.find(type);
    Without the typename keyword, according to C++ standard, iterator should be interpreted as a static member of CreatorMap or something like that.

    MSVC doesn't require that (as an extension?).
    Last edited by anon; 04-17-2009 at 02:53 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Quote Originally Posted by anon View Post
    MSVC doesn't require that (as an extension?).
    Due toe the way it parses the code more likely, I would say.
    But yes, VC++ is known not to be the best strict compiler when it comes to adhering to the standard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2005
    It depends on which version of VC++ you use. Older versions required you to do it the wrong way (without the typename). Newer versions fixed that, although I'm not sure whether they also allow the old way for backwards compatibility.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Thanks Anon! That was the problem exactly.

    For others: this was done using MSVS 2008...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Works on PC, not on UNIX
    By Decrypt in forum C++ Programming
    Replies: 14
    Last Post: 04-20-2006, 01:13 PM
  2. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  3. Programs works one way, not another. VC++ 6.0 help.
    By Cheeze-It in forum Windows Programming
    Replies: 4
    Last Post: 12-10-2002, 10:29 PM
  4. Results for the Encryption Contest -- June 23, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM
  5. My computer works just fine
    By Betazep in forum A Brief History of
    Replies: 10
    Last Post: 07-02-2002, 08:51 AM