此散列仅适用于枚举类型 [英] This hash only works for enumeration types

查看:654
本文介绍了此散列仅适用于枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a (very) simple class in C++ that has a unordered_map member:

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 ++标准库的功能文件,其中包含以下消息:

When I compile, I get no errors in the class's source, but clang fails, pointing me to the C++ standard library's functional file, with the following message:

功能:2441:5 :Static_assert failedThis hash only work for enumeration types

我假设 unordered_map 需要的散列函数有一个问题(如果我是正确的,它被实现为HashMap)。当我用一个简单的 map 替换它,一切都编译正确。我有其他 unordered_map 在我的代码库与 std :: string 作为键完全编译。

I assume that there is a problem with the hashing function needed for unordered_map (which, if I am correct, is implemented as a HashMap). When I replace it with a simple map, everything compiles properly. I have other unordered_maps in my codebase with std::string as keys that compile perfectly.

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

Is there some obvious mistake I've not spotted? I'm highly doubt this would be a standard library bug.

(如果可以有任何帮助,代码编译为 clang- 700.0.65 Apple LLVM版本7.0.0 ,随Xcode 7提供),没有异常/ RTTI和 std = c ++ 14

(if it can be of any help, the code is compiled with clang-700.0.65 (Apple LLVM version 7.0.0, shipped with Xcode 7), no exceptions/RTTI, and std=c++14)

编辑

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

As pointed out by @dau_sama's answer, <string> wasn't included directly (only by including another header), which was causing the problem. The possible duplicate is relatively different, as std::string is not a custom class and the standard library provides a hash function for it.

推荐答案

您可能缺少字符串头

#include <string>

应该可以解决您的问题

这篇关于此散列仅适用于枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆