简体   繁体   English

std::unordered_map 使用枚举和定义的类

[英]std::unordered_map using enum and defined class

I'm trying to define a std::unordered_map using an enum class as the key and a defined class as the referenced object:我正在尝试使用enum class作为键和定义的类作为引用对象来定义std::unordered_map

std::unordered_map<Dimension, unit, EnumClassHash> SI_Dim;
SI_Dim[Dimension::MASS] = BaseSIUnits::kilogram;

Dimension is an enum class declared in a separate header file as Dimension是一个enum class ,在单独的头文件中声明为

enum class Dimension{MASS, TIME, LENGTH, TEMPERATURE, CURRENT, QUANTITY, ANGLE, FORCE, ENERGY, POWER,
                 AREA, VOLUME, NONDIMENSIONAL};

with EnumClassHash as a hashing function (which I can post the code for if it's relevant).使用EnumClassHash作为散列函数(如果相关,我可以发布代码)。

BaseSIUnits::kilogram is defined a few lines above this as BaseSIUnits::kilogram在此之上的几行定义为

const unit BaseSIUnits::kilogram = unit(1, "kg", Dimension::MASS);

which compiles just fine.编译得很好。 But the SI_Dim[Dimension::MASS] = BaseSIUnits::kilogram;但是SI_Dim[Dimension::MASS] = BaseSIUnits::kilogram; line gives me an error.行给了我一个错误。 In QtCreator (my IDE) it says "expected a declaration" and g++ gives the error "SI_Dim does not name a type."在 QtCreator(我的 IDE)中,它说“需要声明”并且 g++ 给出错误“SI_Dim 没有命名类型”。 Neither of these make any sense to me.这些对我来说都没有任何意义。 Also, when looking at the line in QtCreator, neither Dimension::MASS or BaseSIUnits::kilogram is highlighted (almost as if they're unrecognized, even though I know they are).此外,当查看 QtCreator 中的行时, Dimension::MASSBaseSIUnits::kilogram都没有突出显示(几乎好像它们未被识别,即使我知道它们是)。 I don't have much experience with std::unordered_map, so this is probably some simple syntax error I'm missing.我对 std::unordered_map 没有太多经验,所以这可能是我遗漏的一些简单的语法错误。 But the syntax looks right to me based on examples I've looked at.但是根据我看过的例子,语法对我来说是正确的。

As Praetorian noted in the comment, you can't really have code outside of functions in C++ (except for initializing global/static variables).正如 Praetorian 在评论中指出的那样,您实际上不能在 C++ 中拥有函数之外的代码(初始化全局/静态变量除外)。 If you need such code (ie, code that cannot be written as initialization), then you can write a function init_si_dim which is required to be called (once) prior to use.如果你需要这样的代码(即不能写成初始化的代码),那么你可以写一个函数init_si_dim ,它需要在使用前调用(一次)。 See this question for a way of automating this through a global object of an ad-hoc class that does the initialization in the constructor.请参阅此问题,了解通过在构造函数中进行初始化的临时类的全局对象来自动执行此操作的方法。

On a separate direction, the use of an unordered map for your type of enum , seems a bit odd.在另一个方向上,为您的enum类型使用无序映射似乎有点奇怪。 Given that you have so few values, and are not assigning specific integer values to the enum values, I think that you're better off with a random-access container, eg, through std::array .鉴于您的值很少,并且没有为enum值分配特定的整数值,我认为您最好使用随机访问容器,例如通过std::array

Consider the following code考虑以下代码

#include <array>                                                                                                                            

enum class dimension{mass, time, length};

inline constexpr std::size_t dimension_to_index(dimension d)
{
    return static_cast<std::size_t>(d) - static_cast<std::size_t>(dimension::mass);
}

struct foo{};

std::array<foo, 1 + dimension_to_index(dimension::length)> v;

int main()
{
    v[dimension_to_index(dimension::time)];
}   

We first have the enum , then a safe function for converting its values to indices.我们首先有enum ,然后是将其值转换为索引的安全函数。 We then define an array object.然后我们定义一个array对象。 As you can see in main , it's possible now to access the array elements via the enum values.正如您在main中看到的,现在可以通过enum值访问array元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM