在映射c++中,使用enum作为键来设置和获取值
Set and get value with enum as a key in map c++
所以我想做的是将枚举映射到对象的指针。这是我当前的代码:
enum state {A,B,C};
class imageTexture {
public:
imageTexture(std::string path) {};
};
int main() {
std::map<state, imageTexture*> theMap;
theMap[A] = new imageTexture("a");
return 0;
}
联机版本:http://ideone.com/v9HA4h
这是它停止工作的地方。完整错误为:
地图没有可行的过载操作员[]
我对此做了一些研究,发现了一些常数,但我可以很好地克服这个错误。我还浏览了一些地图的示例代码,并感到更加困惑:
std::map<string, int> theMap;
theMap['A'] = 1;
这和我所做的一样,但我的不起作用。有人能帮我吗?如有任何解释,不胜感激。
编辑:更新代码中我有问题的部分
编辑2:我在网上试过这个代码,它有效。然而,它不在我的笔记本电脑里。这只是我的编译器不能使用c++11的问题吗?
查看您的代码或帖子,我认为您缺少了一些重要的东西。这个代码工作得很完美:
#include <iostream>
#include <map>
enum state { RED, YELLOW, GREEN };
class foo
{
int a;
};
int main()
{
std::map<state, foo *> theMap;
theMap[RED] = new foo();
std::cout << "That's all" << std::endl;
}
实例
已编辑:它也适用于C++98(-std=C++98)
相关文章:
- 如何获取进程以设置其亲和力?
- 如何获取控制面板设置值,如字体大小
- 获取和设置函数 c++
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 在 C++ 中将函数获取和设置为虚拟函数
- Qt 原子 int 获取并立即设置
- 提升获取文本的区域设置"Conversion failed"
- 如何使用C++获取/设置OBS中的垂直滚动过滤器属性?
- 在通过 P/Invoke 获取的 C++ 结构上设置 C# 回调
- 如何在Windows中获取和设置系统音量
- 如何设置获取类的私有成员:寻找更好的方法
- 在 c++ 中为我的类设置/获取方法时遇到问题
- 通过 C/C++ 使用 JPEG 像素格式在 v4l2 中设置/获取相机 JPEG 压缩质量
- C++字符串和设置/获取函数
- 如何制作一个好的设置/获取方法
- 设置获取指针
- 在c++中通过指针设置/获取值
- 设置/获取 Java 列表<>从 C 代码
- 在窗口模式下设置/获取我的绝对鼠标位置
- 使用复共轭来设置/获取数组值的代理