繁体   English   中英

如何从CPP中的头文件访问枚举

[英]How to access enum from a header file in CPP

详细信息:C ++,gcc编译器。

说我有一个头文件

    :
class  myClass {
   public: 
    enum color {red, blue};
    :

如何在源文件中设置变量颜色,其中包括文件并声明了该颜色

myClass T;

由于某些原因

我不能将其设置为T.color = red;

我懂了

error: cannot refer to type member ‘color’ in
      ‘something::myClass’ with '.'
  T.color = red;
    ^
<path of header file>:77:7: note: 
      member ‘color’ declared here
        enum color {red, blue};
             ^

我知道我在这里做错了..如果有人可以告诉我什么,这将有很多帮助。

enum color {red, blue}; 定义类型enum color但不定义字段color 您需要通过enum color field;声明一个enum color field; enum {red, blue} color;

这是代码作品

class  myClass {
   public: 
    enum color_t {red, blue};
    enum color_t color;
    // enum {red, blue} color; // or this
};

int main() {
    myClass my;
    my.color = myClass::red;
    my.color = myClass::blue;
    return 0;
}

暂无
暂无

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

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