在 c++ 中共享枚举过课

发布于 2024-12-15 02:47:51 字数 547 浏览 2 评论 0原文

我的程序中有一些枚举,我想在不同的类之间共享它。

当我尝试在每个类中定义它时,我收到了“重新定义”错误。

然后我搜索了谷歌,发现我应该把它放在不同的标题中。 我尝试这样做并将标头包含到每个类标头中 - 我仍然遇到相同的错误。

又搜索了一些,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我还不会笑 2024-12-22 02:47:51

你还记得多重包含守卫吗?通常看起来像:

#ifndef MY_HEADER_FILE_H
#define MY_HEADER_FILE_H
[...code...]
#endif

并保护类型和枚举免于在编译单元中定义乘法。

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.

娇妻 2024-12-22 02:47:51

如果您愿意,您只需在标头中声明一次枚举,并在使用枚举的位置包含该标头:

//enum.h:
//include guards:
#ifndef MY_ENUMS
#define MY_ENUMS
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};
}
#endif

//A.h

#include "enum.h"
class A
{
   void check()
   {
      my_enums::skill val = my_enums::SCRIPT;
   } 
};

You only need to declare your enums once in a header if you wish and include that header where you use the enums:

//enum.h:
//include guards:
#ifndef MY_ENUMS
#define MY_ENUMS
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};
}
#endif

//A.h

#include "enum.h"
class A
{
   void check()
   {
      my_enums::skill val = my_enums::SCRIPT;
   } 
};
残龙傲雪 2024-12-22 02:47:51

也许您已经多次包含它?

不要忘记在 include 上添加“守卫”

#ifndef MY_ENUM_H_
#define MY_ENUM_H_

.... enter your enums here ...
#endif // MY_ENUM_H_

Maybe you've included it more then once?

Don't forget to add "guards" on include

#ifndef MY_ENUM_H_
#define MY_ENUM_H_

.... enter your enums here ...
#endif // MY_ENUM_H_
開玄 2024-12-22 02:47:51
struct Classification
{
    enum Enum{ dataStorage, dms, eCommerce, gaming, rtes, security }
};

struct Skill
{
    enum Enum{ cpp, java, script, web, system, qa };
};

struct CompanyPolicy
{
    enum Enum{ cheap, lavish, cost_effective };
};

void whatever( Classification::Enum classification ) {}

class A
    : protected Classification
{
public:
    void foo() { whatever( dataStorage ); }
};

class B
{
public:
    void foo() { whatever( Classification::dataStorage ); }
};
struct Classification
{
    enum Enum{ dataStorage, dms, eCommerce, gaming, rtes, security }
};

struct Skill
{
    enum Enum{ cpp, java, script, web, system, qa };
};

struct CompanyPolicy
{
    enum Enum{ cheap, lavish, cost_effective };
};

void whatever( Classification::Enum classification ) {}

class A
    : protected Classification
{
public:
    void foo() { whatever( dataStorage ); }
};

class B
{
public:
    void foo() { whatever( Classification::dataStorage ); }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文