【问题标题】:How to use enum in structure C++如何在结构 C++ 中使用枚举
【发布时间】:2013-08-10 20:41:38
【问题描述】:

我正在尝试在结构中使用枚举,但出现以下错误:

union.cpp:27:21: error: ‘DOLLAR’ was not declared in this scope
 book.currency = DOLLAR;
                 ^

这是我的代码:

 struct shelf{
      char title[50];
      char author[50];
      union {
          float dollars;
          int yens;
      };
 
      enum {
          DOLLAR = 1, YEN
      } currency;
  } book;
 
  int main () {
      strcpy(book.title,"book 1");
      strcpy(book.author, "author 1");
 
      book.dollars = 100;
 
      book.currency = DOLLAR;
 
      cout << book.currency;
      return 0;
  }
 

【问题讨论】:

  • 只在结构架子内部定义
  • 使用shelf::DOLLAR 或在struct shelf 之外声明enum currency_enum,然后在结构架中制作`currency_enum currency`;
  • 避免使用ALLCAPS,因为它通常用于#define 令牌。

标签: c++ struct enums


【解决方案1】:
book.currency = DOLLAR;

应该是

book.currency = shelf::DOLLAR;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 2014-06-21
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 2016-01-16
    • 2013-02-11
    • 2021-05-21
    相关资源
    最近更新 更多