在 c++ 中共享枚举过课
我的程序中有一些枚举,我想在不同的类之间共享它。
当我尝试在每个类中定义它时,我收到了“重新定义”错误。
然后我搜索了谷歌,发现我应该把它放在不同的标题中。 我尝试这样做并将标头包含到每个类标头中 - 我仍然遇到相同的错误。
又搜索了一些,在 StackOverflow 中发现了一个线程,说我应该将它们放在自己的命名空间中。 所以我尝试了:
enum.h:
namespace my_enums
{
enum classification {DATA_STORAGE,DMS,E_COMMERCE,GAMING,RTES,SECURITY};
enum skill { CPP, JAVA, SCRIPT, WEB, SYSTEM, QA };
enum company_policy {CHEAP, LAVISH, COST_EFFECTIVE};
}
但这仍然不起作用:首先,如果告诉包含标头的类:“using namespace my_enums;”我收到“不明确”错误。
做我想做的事情的正确方法是什么?
先谢谢了^_^
I have a few enums in my program and I want to share it across different classes.
When I tried to define it in each class I got a "redefining" error.
Then I searched Google and saw that I should put it in a different header.
I tried to to do that and included the header into every class header - I still got the same error.
Searched some more and found in StackOverflow a thread saying I should put them in their own namespace.
So I tried:
enum.h:
namespace my_enums
{
enum classification {DATA_STORAGE,DMS,E_COMMERCE,GAMING,RTES,SECURITY};
enum skill { CPP, JAVA, SCRIPT, WEB, SYSTEM, QA };
enum company_policy {CHEAP, LAVISH, COST_EFFECTIVE};
}
But that still doesn't work: First, if tell classes that include the header to: "using namespace my_enums;" I get " is ambiguous" error.
What is the correct way to do what I'm trying to do?
Thanks in advance ^_^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你还记得多重包含守卫吗?通常看起来像:
并保护类型和枚举免于在编译单元中定义乘法。
Did you remember the multiple inclusion guard? Normally looks like:
and protects types and enums from getting defined multiply in a compilation unit.
如果您愿意,您只需在标头中声明一次枚举,并在使用枚举的位置包含该标头:
You only need to declare your enums once in a header if you wish and include that header where you use the enums:
也许您已经多次包含它?
不要忘记在 include 上添加“守卫”
Maybe you've included it more then once?
Don't forget to add "guards" on include