【发布时间】: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令牌。