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

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    42

    Compile issue in g++, works fine in VS

    Hi
    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

    Thanks
    Ken

    factory.h
    Code:
     #ifndef FACTORY_
    #define FACTORY_
    
    #include <map>
    #include <string>
    
    
    typedef std::string ObjectType;
    
    
    template<class Base>
    class CreatorBase
    {
    public:
        virtual ~CreatorBase() {}
        virtual Base * Create() const = 0;
    };
    
    
    
    template<class Product, class Base>
    class Creator : public CreatorBase<Base>
    {
    public:
        virtual Base * Create() const {return new Product;}
    };
    
    
    
    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;
        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_*/
    entity.h
    Code:
    #ifndef ENTITY_H_
    #define ENTITY_H_
    
    class GameEntity
    {
    public:
        virtual ~GameEntity() {}
        virtual const char * GetClassName() { return "GameEntity"; }
    
    };
    
    
    class GameCamera : public GameEntity
    {
    public:
        virtual const char * GetClassName() { return "GameCamera"; }
    
    };
    
    #endif /*ENTITY_H_*/
    main.c++
    Code:
    #include <iostream>
    
    #include "factory.h"
    #include "entity.h"
    
    int 
    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
    Posts
    3,573
    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:

    Code:
        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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Posts
    7,366
    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
    Posts
    42
    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 Cprogramming.com
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM
  5. My computer works just fine
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 07-02-2002, 08:51 AM