帮助纠正源代码,带有模板

发布于 2024-07-10 21:15:39 字数 1460 浏览 3 评论 0原文

我尝试编译发布的示例(C++ 服务提供程序),但未能编译到 VS8 VC9。 我对模板的经验很少。
有什么建议吗?
坦克。

这些是错误:
Dictionarystl.cpp(40) : 错误 C2663: 'std::_Tree<_Traits>::find' : 2 个重载对于 'this' 指针没有合法转换
Dictionarystl.cpp(48) : 错误 C2679: 二进制 '[' : 找不到采用 'const type_info *__w64 ' 类型的右侧操作数的运算符(或者没有可接受的转换)

#include <typeinfo>
#include <map>
#include <string>
using namespace std;

class SomeClass
{
public:
    virtual ~SomeClass() {} // virtual function to get a v-table
};

struct type_info_less
{
    bool operator() (const std::type_info* lhs, const std::type_info* rhs) const
    {
        return lhs->before(*rhs) != 0;
    }
};

class TypeMap
{
    typedef map <type_info *, void *, type_info_less> TypenameToObject;
    TypenameToObject ObjectMap;

public:
    template <typename T> 
    T *Get () const
    {
        TypenameToObject::const_iterator iType = ObjectMap.find(&typeid(T));
        if (iType == ObjectMap.end())
            return NULL;
        return reinterpret_cast<T *>(iType->second);
    }
    template <typename T> 
    void Set(T *value) 
    {
        ObjectMap[&typeid(T)] = reinterpret_cast<void *>(value);
    }
};

int main()
{
    TypeMap Services;
    Services.Set<SomeClass>(new SomeClass());
    SomeClass *x = Services.Get<SomeClass>();
}

I tried to compile the example posted (C++ Service Providers) and failed to VS8 VC9. I have little experience with template.
Any suggestions?
Tanks.

These are the errors :
dictionarystl.cpp(40) : error C2663: 'std::_Tree<_Traits>::find' : 2 overloads have no legal conversion for 'this' pointer
dictionarystl.cpp(48) : error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const type_info *__w64 ' (or there is no acceptable conversion)

#include <typeinfo>
#include <map>
#include <string>
using namespace std;

class SomeClass
{
public:
    virtual ~SomeClass() {} // virtual function to get a v-table
};

struct type_info_less
{
    bool operator() (const std::type_info* lhs, const std::type_info* rhs) const
    {
        return lhs->before(*rhs) != 0;
    }
};

class TypeMap
{
    typedef map <type_info *, void *, type_info_less> TypenameToObject;
    TypenameToObject ObjectMap;

public:
    template <typename T> 
    T *Get () const
    {
        TypenameToObject::const_iterator iType = ObjectMap.find(&typeid(T));
        if (iType == ObjectMap.end())
            return NULL;
        return reinterpret_cast<T *>(iType->second);
    }
    template <typename T> 
    void Set(T *value) 
    {
        ObjectMap[&typeid(T)] = reinterpret_cast<void *>(value);
    }
};

int main()
{
    TypeMap Services;
    Services.Set<SomeClass>(new SomeClass());
    SomeClass *x = Services.Get<SomeClass>();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木落 2024-07-17 21:15:39

为了编译此代码,以下行:

typedef map<type_info *, void *, type_info_less> TypenameToObject;

应该是:

typedef map<const type_info *, void *, type_info_less> TypenameToObject;

For this code to compile, the following line:

typedef map<type_info *, void *, type_info_less> TypenameToObject;

should be:

typedef map<const type_info *, void *, type_info_less> TypenameToObject;
旧梦荧光笔 2024-07-17 21:15:39

将第 33 行的 typedef 更改为:

typedef map <const type_info *, void *, type_info_less> TypenameToObject;

这至少可以修复您的第二个错误。 我无法重现您的第一个错误,但我怀疑这也能解决这个问题。

Change the typedef on line 33 to read:

typedef map <const type_info *, void *, type_info_less> TypenameToObject;

That will at least fix your second error. I couldn't reproduce your first error, but I suspect that this will fix that, as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文