帮助纠正源代码,带有模板
我尝试编译发布的示例(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了编译此代码,以下行:
应该是:
For this code to compile, the following line:
should be:
将第 33 行的
typedef
更改为:这至少可以修复您的第二个错误。 我无法重现您的第一个错误,但我怀疑这也能解决这个问题。
Change the
typedef
on line 33 to read: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.