C++ 如何在结构C++;

C++ 如何在结构C++;,c++,struct,enums,C++,Struct,Enums,我试图在结构中使用enum,但出现以下错误: 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; i

我试图在结构中使用enum,但出现以下错误:

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;
  }
 
struct shelf{
字符标题[50];
char作者[50];
联合{
浮动美元;
内延;
};
枚举{
美元=1,日元
}货币;
}书;
int main(){
strcpy(书名,“第1册”);
strcpy(book.author,“author 1”);
美元=100;
记账本位币=美元;
库特
应该是

book.currency = shelf::DOLLAR;

它只在struct shelfse
shelf::DOLLAR
内部定义,或者在
struct shelf
外部声明
enum currency\u enum
,然后在struct shelf中创建“currency\u enum currency”;避免使用
ALLCAPS
,因为这通常用于
\define
令牌。
book.currency = shelf::DOLLAR;