[英]Enum type variable declarations in C
I declared this enum type in C : 我在C中声明了这个枚举类型:
enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC } ;
When I try to create a variable of type months in main()
with: 当我尝试使用以下方法在
main()
创建类型为months的变量时:
months month;
It gives the following error: 它给出以下错误:
unknown type 'months'
未知类型“月”
But when I declare it like this: 但是当我这样声明时:
enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC } month;
It works fine. 工作正常。 I thought both ways were valid, so why is there an error?
我认为这两种方法都是有效的,那么为什么会出现错误呢?
You need to wrap a typedef
around it, otherwise you can access it by stating that it's an enum
. 您需要在其周围包装一个
typedef
,否则可以通过声明它是一个enum
来访问它。
Example: 例:
typedef enum { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC } months;
months month;
Or 要么
enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC };
enum months month;
Instead of 代替
months month;
you have to write 你必须写
enum months month;
The other way is to define a typedef for the enumeration. 另一种方法是为枚举定义一个typedef。 For example
例如
typedef enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC } months;
and then you may write 然后你可以写
months month;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.