VS 2010带有工厂设计模式的编译问题 [英] VS 2010 compiling question with factory design pattern

查看:46
本文介绍了VS 2010带有工厂设计模式的编译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直通过以下代码获取编译错误消息:

Hi,

I kept get compiling error messages with following code:

getMap()->insert(std::make_pair(s, &createT<t>));</t>



1> C:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ include \ type_traits(197):错误C2752:``std :: tr1 :: __ Remove_reference< _Ty>'':一个以上的部分专业化与模板参数列表匹配

如何解决呢?

谢谢,






1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\type_traits(197): error C2752: ''std::tr1::_Remove_reference<_Ty>'' : more than one partial specialization matches the template argument list

How can fix this?

Thanks,




class CGeometry;
template<typename T> CGeometry * createT() { return new T; };
struct BaseFactory
{
    typedef std::map<std::string, CGeometry*(*)()> map_type;
    static void RemoveAll()
    {
        if( map ) map->clear();
    }
    static CGeometry * createInstance(std::string const& s)
    {
        map_type::iterator it = getMap()->find(s);
        if(it == getMap()->end())
            return 0;
        return it->second();
    }
protected:
    static map_type * getMap()
    {
        // never delete'ed. (exist until program termination)
        // because we can't guarantee correct destruction order
        if(!map) { map = new map_type; }
        return map;
    }
private:
    static map_type * map;
};
template<typename T>
struct RegisterUserClass : BaseFactory
{
    RegisterUserClass( std::string const& s )
    {
        getMap()->insert(std::make_pair(s, &createT<T>));
    };
};

推荐答案



我怀疑您在使用VC2010编译代码时忘记了#include <string>.

BTW C ++可以在编译时做很多事情,但是您似乎更喜欢在运行时完成它们:叹气:
例如:
Hi,

I suspect you forgot to #include <string> as your code compiles with VC2010.

BTW C++ can do lot of things at compile time but you seem to prefer having them done at runtime :sigh:
For instance:
struct BaseFactory
{
    typedef std::map<std::string, CGeometry*(*)()> map_type;
    static void RemoveAll()
    {
        map.clear();
    }
    static CGeometry * createInstance(std::string const& s)
    {
        map_type::iterator it = map.find(s);
        return it == map.end() ? 0 : it->second();
    }
protected:
    static map_type map;
};

等效于您的构造,更简单,更高效.

欢呼声,
AR

is equivalent to your construct, simpler and more efficient.

cheers,
AR


对不起,代码可以自己编译.
但是当其他类使用该类时,将产生编译错误.
在VS 2008上可以正常工作.错误仅在VS 2010上发生.
该错误仅在以下代码中发生.

Sorry, the code can be compiled by itself.
But when other class use this class, it will generates compile error.
It works just fine with VS 2008. The error only happens with VS 2010.
The error only happens with following code.

getMap()->insert(std::make_pair(s, &createT<t>));
</t>



可以编译.我包括了它所需要的一切.
谢谢



It can be compiled. I include everything it needs.
Thanks


这篇关于VS 2010带有工厂设计模式的编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆