c++ - 此哈希仅适用于枚举类型

标签 c++ xcode stl clang unordered-map

我正在使用 C++ 开发一个(非常)简单的类,它有一个 unordered_map成员:

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;
};

当我编译时,我在类的源代码中没有发现任何错误,但 clang 失败,将我指向 C++ 标准库的 functional文件,包含以下消息:

functional:2441:5: Static_assert failed "This hash only works for enumeration types"

stack trace from Clang

我假设 unordered_map 所需的哈希函数有问题(如果我是正确的,它是作为 HashMap 实现的)。当我用一个简单的 map 替换它时,一切都正确编译。我还有其他unordered_map在我的代码库中有 std::string作为完美编译的 key 。

是否有一些我没有发现的明显错误?我非常怀疑这是一个标准库错误。

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

编辑

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

最佳答案

您可能缺少字符串标题

#include <string>

应该可以解决你的问题

关于c++ - 此哈希仅适用于枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32121497/

相关文章:

python - 为什么在python 3.7.2中使用PyImport_ImportModule segfault?

c++ - C++ 中不同大小的不同行为(Firebreath 源代码)

c++ - 通过包含模板的继承链的 static_cast 将无法编译

C++ 构造函数语法和零初始化

iphone - 子项目的 Xcode 环境变量

ios - iTunes Connect - 新版本未显示在 TestFlight (Xcode 7.1) 中

objective-c - 使用后退按钮将数据传递到 ViewController

c++ - 将指向堆对象的指针存储在 STL 容器中以供以后重新分配

c++ - map::count 后跟 map::operator[] 的效率

c++ - 解释一下默认的priority_queue::top()的行为?