[英]Refer to an enum inside a struct
I have an struct defined like this in a header file: 我在头文件中定义了这样的结构:
struct MessageA {
enum Status {
eReady, eNotReady
};
};
Later, when I try to use this enum: 稍后,当我尝试使用此枚举时:
#include <MessageA.hh>
...
if( status != MessageA.Status.eReady ) continue;
I get the error: 我收到错误:
expected primary-expression before '.' token
'Status' has not been declared
I tried the following and it worked: 我尝试了以下方法,它起作用了:
if( status != MessageA::eReady ) continue;
However, if use: 但是,如果使用:
if( status != MessageA::Status::eReady ) continue;
Then I get the error: 然后我得到错误:
Status is not a class or a namespace
If I needed to specify the name of the enum fully qualified (such as if there were multiple enums with the same values inside) how should I do it? 如果我需要指定完全限定的枚举的名称(例如,如果内部有多个枚举且具有相同值的枚举),该怎么办?
Use the scope operator: 使用范围运算符:
MessageA::Status::eReady;
Also note that prior to C++11 labels of enums were not scoped in which case you would be using the following code: 还要注意,在C ++ 11之前,枚举的标签没有范围,在这种情况下,您将使用以下代码:
MessageA::eReady;
Put ;
放
;
in end of the struct
: 在
struct
末尾:
struct MessageA {
enum Status {
eReady, eNotReady
};
};
Then use of enum
elements like bellow : 然后使用诸如下面的
enum
元素:
int main()
{
if (MessageA::Status::eNotReady == 0)
printf("ok");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.