开发者

How can I fix my Factory Pattern to eradicate these compile errors?

开发者 https://www.devze.com 2023-01-26 16:35 出处:网络
My goal is to create a system wherein I can provide the string name of an class at run time and have it return an instance of that class in turn.

My goal is to create a system wherein I can provide the string name of an class at run time and have it return an instance of that class in turn. Searching stackoverflow, I came across an example that seems to do exactly what I am trying to accomplish, although I am currently unable to have it compile properly. The following is based on that code:

//LevelObject.h    
#pragma once

#include <map>
#include <string>

class LevelObject
{
    protected:
        int ID;

    public:
        template<class T> static LevelObject* createT(void)
        {
            return new T(0);
        }

        LevelObject(void);
        ~LevelObject(void);
};

struct BaseFactory
{
    typedef std::map<std::string, LevelObject*(*)()> map_type;

    static LevelObject* createInstance(const std::string& s)
    {
        map_type::iterator it = getMap()->find(s);
        if(it == getMap()->end())
        {
            return 0;
        }
        return it->second();
    }

    private:
        static map_type* objectMap;

    protected:
        static map_type* getMap()
        {
            if(!objectMap)
            {
                objectMap= new map_type;
            } 
            return objectMap; 
        }
};

template<class T>
struct DerivedRegister : BaseFactory
{ 
    DerivedRegister(const std::string& s)
    { 
        getMap()->insert(std::make_pair( s, &LevelObject::createT<T> ));
    }
};


//Item.h
#pragma once

#include "LevelObject.h"

class Item :
    public LevelObject
{
    int ID;
    static DerivedRegister<Item> reg;

public:
    Item(int id);
    ~Item(void);
};


//Item.cpp
#include "Item.h"

Item::Item(int id)
{
    ID = id;
}

Item::~Item(void)
{
}

DerivedRegister<Item> Item::reg("item");

The logic is that the derived objects, i.e. Item, will register a string and reference to a function that returns an instance of itself. On calling createInstance, it will take in a user inputted string and use the map to determine the object to return.

Unfortunately, this code is not compiling correctly, and gives me the following errors:

Error 1 error C2752: 'std::tr1::_Remove_reference<_Ty>' : more than one partial specialization matches the template argument list

Error 2 error C2528: 'abstract declarator' : pointer to reference is illegal c:\program files\microsoft visual studio 10.0\vc\include\type_traits 965

Error 3 error C2528: 'type' : pointer to reference is illegal c:\program files\microsoft visual studio 10.0\vc\include\type_traits 349

If someone can开发者_开发问答 help smooth out these errors, I would greatly appreciate it. Or perhaps I am going about this entirely wrong in the first place, so if someone instead feels that I should be going in a different direction entirely please let me know.

Thanks in advance.


It's been a long time since this question was posted, but since there's no answer and I stumbled here too, I figured I'd add one. I copied the same factory code you did (from the StackOverflow answer here) and had the same problem. I found the solution at this StackOverflow answer.

It turns out Visual Studio 2010 (which I'm assuming you're using) has a problem with std::make_pair. Just use std::pair<std::string,LevelObject*(*)()> instead and you'll be good to go. At least that resolved this exact same problem for me.


I added empty bodies to the LevelObject class constructor and destructor:

LevelObject(void) { }
~LevelObject(void) { }

Then declared the static map member variable of the BaeFactory class:

BaseFactory::map_type* BaseFactory::map;

and the code compiled without errors in both GCC and Visual Studio.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号