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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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