我在C:
中声明了这个枚举类型enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC } ;
当我尝试在main()
中使用:
months month;
它出现以下错误:
未知类型'月'
但是当我宣布这样的时候:
enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC } month;
工作正常。我认为两种方式都有效,为什么会出错?
答案 0 :(得分:2)
您需要在其周围包裹typedef
,否则您可以通过声明它是enum
来访问它。
示例:
typedef enum { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC } months;
months month;
或者
enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC };
enum months month;
答案 1 :(得分:1)
而不是
months month;
你必须写
enum months month;
另一种方法是为枚举定义typedef。例如
typedef enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC } months;
然后你可以写
months month;