以enum作为键的std::map
C++ - std::map with an enum as key
我目前正在尝试创建一个std::map来存储键盘每个键的状态。为此,我创建了以下映射:
static std::map<enum MouseCode, enum InputState> mousePressedMap;
这里没有问题。
为了注册状态,我创建了一个由主类调用的函数:
void Input::ReadUserInput(enum Input::MouseCode mouseCode, enum Input::InputState inputState) {
mousePressedMap[mouseCode] = inputState;
}
在这里我得到了我的问题:我在mousePressedMap的"["上出现了错误,说:没有"[]"操作符对应于这些操作数。我也不能这样做:
void Input::ReadUserInput(enum Input::MouseCode mouseCode, enum Input::InputState inputState) {
mousePressedMap[MouseCode::LeftButton] = InputState::DOWN;
}
我得到了相同的错误。
此外,我认为错误不是来自我的枚举:
/// <summary>
/// <para>Enumeration to set parameters to the mouse buttons handling function. The mouse code defines which button has been called.</para>
/// </summary>
enum MouseCode {
LeftButton = 0,
MiddleButton = 1,
RightButton = 2
};
/// <summary>
/// <para>Enumeration to set parameters to the mouse buttons handling function. The input state is the current state of the button</para>
/// </summary>
enum InputState {
/// <summary>
/// Just pressed.
/// </summary>
DOWN,
/// <summary>
/// Just released.
/// </summary>
UP,
/// <summary>
/// Was already pressed the last frame.
/// </summary>
PRESSED,
/// <summary>
/// Is not currently activated and wasn't the last frame.
/// </summary>
INACTIVE
};
谁能给我解释一下为什么?我来自c#和Java语言,在这些语言中,这种语法可以完美地工作,而这里的情况却不是这样,这很奇怪。
谢谢你的帮助!
EDIT 1:错误信息是(法语,抱歉…):Win7 DirectX c:users maximum desktop DirectX tests Win7 DirectX Win7 DirectX Input .cpp 16
那么,代码的目的是将值0、1和2映射到其他值?使用由MouseCode
值索引的数组
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 使用一个考虑到std::map中键值的滚动或换行的键
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- C++如何创建 std::map
- 从其他容器中移动构造"std::map"
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 使用重载 [] 运算符返回 std::map() 的可赋值
- std::map, std::unordered_map - 缩小初始值设定项列表中的转换范围
- C++ 使用枚举类对象分配 std::map 值
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- std::map:当元素不可默认构造时创建/替换元素
- Arduino编译器和STL:使用std::vector和std::map