简体   繁体   English

c ++定义不同命名空间的枚举

[英]c++ defining enum of different namespace

I have the following header file: 我有以下头文件:

namespace First
{
    namespace Second
    {
        class Limit
        {
            public:
                enum Status
                {
                    GOOD,
                    BAD
                };
        }
    }
}

Since it's proprietary I have changed the names, and only left the relevant info. 由于它是专有的,我更改了名称,只留下了相关信息。

In my own class I have... 我自己上课了......

First::Second::Limit::Status limit_status = First::Second::Limit::Status::OK; First :: Second :: Limit :: Status limit_status = First :: Second :: Limit :: Status :: OK;

But I get an error: error: class First::Second::Limit::Status is not a class or namespace 但是我得到一个错误:错误:类First :: Second :: Limit :: Status不是类或命名空间

I am able to declare a variable of that enum, but not set it to any of the values. 我能够声明该枚举的变量,但不能将其设置为任何值。 How do I fix this? 我该如何解决?

First::Second::Limit::Status limit_status = First::Second::Limit::GOOD;

You don't need the Status bit. 您不需要Status位。 Think of it as defining several const int s inside Limit , you wouldn't say Limit::int::GOOD . 可以把它想象为在Limit定义几个const int ,你不会说Limit::int::GOOD

If you have C++11, use enum class : 如果你有C ++ 11,请使用enum class

namespace First
{
    namespace Second
    {
        class Limit
        {
            public:
                enum class Status
                {
                    GOOD,
                    BAD
                };
        }
    }
}

Now you can use Status as a scope. 现在您可以使用Status作为范围。 If not, you'll have to not include the Status:: part. 如果没有,您将不得包含Status:: part。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM