小编fra*_*ung的帖子

如何在结构 C++ 中使用枚举

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

union.cpp:27:21: error: ‘DOLLAR’ was not declared in this scope
 book.currency = DOLLAR;
                 ^
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

 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;
  }
 
Run Code Online (Sandbox Code Playgroud)

c++ enums struct

4
推荐指数
1
解决办法
3571
查看次数

标签 统计

c++ ×1

enums ×1

struct ×1