在c ++中通过类共享枚举 [英] Sharing enums in c++ over classes
问题描述
当我在每个类中定义它时,我得到一个重新定义错误。
然后我搜索Google,看到我应该把它放在另一个标题中。
我试图这样做,并将标题包含到每个类头中 - 我仍然有相同的错误。
在StackOverflow中搜索一些更多的内容说我应该把它们放在自己的命名空间里。
所以我试过:
enum.h:
命名空间my_enums
{
枚举分类{DATA_STORAGE,DMS,E_COMMERCE,GAMING,RTES,SECURITY};
枚举技巧{CPP,JAVA,SCRIPT,WEB,SYSTEM,QA};
enum company_policy {CHEAP,LAVISH,COST_EFFECTIVE};
}
但这仍然不起作用:首先,如果告诉类包括标题:using namespace my_enums;我得到不明确的错误。
正确的方法是做什么?
提前感谢^ _ ^
你还记得多重包围警卫吗?通常情况如下:
#ifndef MY_HEADER_FILE_H
#define MY_HEADER_FILE_H
[... code ... ]
#endif
并保护类型和枚举免于在编译单元中定义乘法。
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 ^_^
Did you remember the multiple inclusion guard? Normally looks like:
#ifndef MY_HEADER_FILE_H
#define MY_HEADER_FILE_H
[...code...]
#endif
and protects types and enums from getting defined multiply in a compilation unit.
这篇关于在c ++中通过类共享枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!