我编写了以下简单函数 - 一个以 2 个 GUID 作为键和值的无序映射。但是,我认为他们没有定义 GUID 比较器,因此它会在我尝试过的所有 GUID 比较器中抛出此错误。
#include <unordered_map>
#include<iostream>
#include<stdio.h>
#include<Objbase.h>
typedef unordered_map<GUID, GUID, less_guid> Mymap;
Mymap c1;
int __cdecl wmain(int /*argc*/, __in_ecount(argc) WCHAR * /*argv[ ]*/)
{
GUID TargetC;
GUID TargetA;
CoCreateGuid(&TargetC); CoCreateGuid(&TargetA);
c1.insert(Mymap::value_type(TargetC,TargetA)); /
getch();
return 0;
}
这是出现的错误: ">c:\program files (x86)\microsoft visual studio 10.0\vc\include\xhash(154): error C2064: term does not evaluate to a function takeing 1 arguments 1> 类未定义“operator()”或用户定义的转换运算符,指向函数指针或函数引用,该函数采用适当数量的参数"
我迫切需要一个解决方案。我会很感激你的意见。谢谢!
最佳答案
std::unordered_map
的第三个模板参数是一个哈希函数,第四个模板参数是一个比较函数:
template<class Key,
class T,
class Hash = hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, T> > >
class unordered_map;
您正在传递 less_guid
作为第三个模板参数,这听起来像是一个比较函数。
关于c++ - GUID 比较器错误!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6699963/