gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:01:30 24 4
gpt4 key购买 nike

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

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;
}

最佳答案

book.currency = DOLLAR;

应该是

book.currency = shelf::DOLLAR;

关于c++ - 如何在结构 C++ 中使用枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18166162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com