此哈希仅适用于枚举类型 我在C++中使用一个非常简单的类,它有一个无序的映射> 成员: class SDLFontManager : public CPObject, public FontManagerProtocol { public: SDLFontManager() {}; // flush the cache when the manager is destroyed virtual ~SDLFontManager() { flushFontCache(); }; // load a font from the cache if it exists, or load it from file and cache it. virtual FontProtocol* fontWithTTF(const char* filename) { if(_cache.find(filename) != _cache.end()) { return _cache[filename]; } SDLFont* font = new SDLFont(filename); _cache[filename] = font; font->retain(); return font; } // flush the font cache virtual void flushFontCache() { for(auto font: _cache) { font.second->release(); } } private: std::unordered_map<std::string, SDLFont*> _cache; }; 类SDLFontManager:public CPObject、public FontManagerProtocol{ 公众: SDLFontManager(){}; //管理器被销毁时刷新缓存 virtual~SDLFontManager(){flushFontCache();}; //从缓存中加载字体(如果存在),或从文件中加载并缓存。 虚拟FontProtocol*fontWithTTF(常量字符*文件名){ if(_cache.find(filename)!=_cache.end()){ 返回_缓存[文件名]; } SDLFont*font=新的SDLFont(文件名); _缓存[文件名]=字体; 字体->保留(); 返回字体; } //刷新字体缓存 虚空 flushFontCache(){ 用于(自动字体:\缓存){ font.second->release(); } } 私人: std::无序的\u映射\u缓存; }; 编译时,我在类的源代码中没有错误,但是CLAN失败了,指向C++标准库的代码>函数< /C>文件,下面的消息:

此哈希仅适用于枚举类型 我在C++中使用一个非常简单的类,它有一个无序的映射> 成员: class SDLFontManager : public CPObject, public FontManagerProtocol { public: SDLFontManager() {}; // flush the cache when the manager is destroyed virtual ~SDLFontManager() { flushFontCache(); }; // load a font from the cache if it exists, or load it from file and cache it. virtual FontProtocol* fontWithTTF(const char* filename) { if(_cache.find(filename) != _cache.end()) { return _cache[filename]; } SDLFont* font = new SDLFont(filename); _cache[filename] = font; font->retain(); return font; } // flush the font cache virtual void flushFontCache() { for(auto font: _cache) { font.second->release(); } } private: std::unordered_map<std::string, SDLFont*> _cache; }; 类SDLFontManager:public CPObject、public FontManagerProtocol{ 公众: SDLFontManager(){}; //管理器被销毁时刷新缓存 virtual~SDLFontManager(){flushFontCache();}; //从缓存中加载字体(如果存在),或从文件中加载并缓存。 虚拟FontProtocol*fontWithTTF(常量字符*文件名){ if(_cache.find(filename)!=_cache.end()){ 返回_缓存[文件名]; } SDLFont*font=新的SDLFont(文件名); _缓存[文件名]=字体; 字体->保留(); 返回字体; } //刷新字体缓存 虚空 flushFontCache(){ 用于(自动字体:\缓存){ font.second->release(); } } 私人: std::无序的\u映射\u缓存; }; 编译时,我在类的源代码中没有错误,但是CLAN失败了,指向C++标准库的代码>函数< /C>文件,下面的消息:,c++,xcode,stl,clang,unordered-map,C++,Xcode,Stl,Clang,Unordered Map,functional:2441:5:Static\u assert失败“此哈希仅适用于枚举类型” 我假设,unordered_map所需的散列函数有问题(如果我是正确的,它是作为散列映射实现的)。当我用一个简单的映射替换它时,一切都可以正常编译。我的代码库中还有其他无序映射s,其中std::string作为键,可以完美编译 有什么明显的错误我没有发现吗?我非常怀疑这是一个标准的库错误 (如果有任何帮助,代码是使用clang-700.0.65(Apple LLVM 7.0.0版,随Xcode 7

functional:2441:5:Static\u assert失败“此哈希仅适用于枚举类型”

我假设,
unordered_map
所需的散列函数有问题(如果我是正确的,它是作为散列映射实现的)。当我用一个简单的
映射替换它时,一切都可以正常编译。我的代码库中还有其他
无序映射
s,其中
std::string
作为键,可以完美编译

有什么明显的错误我没有发现吗?我非常怀疑这是一个标准的库错误

(如果有任何帮助,代码是使用
clang-700.0.65
Apple LLVM 7.0.0版
,随Xcode 7一起提供)、无异常/RTTI和
std=c++14
)编译的。)

编辑


正如@dau_sama的回答所指出的,
没有直接包含(只是通过包含另一个标题),这导致了问题。可能的重复项相对不同,因为
std::string
不是自定义类,标准库为其提供了哈希函数。

您可能缺少字符串头

#include <string>
#包括

应该可以解决您的问题

您可能缺少字符串标题

#include <string>
#包括

应该解决您的问题

您是否将字符串头包含在此编译单元中?可能重复的字符串头包含在“代理”中(包含在包含的头文件中)。这可能是问题所在吗?确实是问题所在,包括
显式地使文件编译。有没有办法将注释标记为答案?@cesarparent没有,我只是将其添加为答案:-)您是否在这个编译单元中包含字符串头?它的可能副本包含在“代理”中(包含在包含的头文件中)。这可能是问题所在吗?确实是问题所在,包括
显式地使文件编译。有没有办法将评论标记为答案?@cesarparent没有,我只是将其添加为答案:-)