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

时间:2013-08-10 20:41:39

标签: c++ struct enums

我正在尝试在结构中使用枚举,但是我收到以下错误:

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.title, "author 1");

      book.dollars = 100;

      book.currency = DOLLAR;

      cout << book.currency;
      return 0;
  }

1 个答案:

答案 0 :(得分:4)

book.currency = DOLLAR;

应该是

book.currency = shelf::DOLLAR;