如何在 switch 与 integer using 中使用枚举
How to use enum in switch vs integer using
我在头文件中有 enum
。
typedef enum{
up = 8, down = 2, left = 4, right = 6
}direction;
我想使用枚举来识别移动类型。
像这样:
void sayTypeOfMove(int type){
switch(type){
case direction.up:
printf("IT IS UP MOVE...");
break;
}
}
代码编译不通过,问题出在哪里?
定义如
typedef enum{
up = 8, down = 2, left = 4, right = 6
}direction;
direction
是一个 类型 ,它不是一个变量。
您需要定义一个该类型的变量,然后使用该值。
记住,枚举根本没有成员变量访问概念。枚举器列表包含 "enumeration-constant" s。你可以直接使用它们,作为一个值。
C 在知道您正在处理枚举时理解枚举元素,因此正确的代码是
void sayTypeOfMove(direction type){
switch(type){
case up:
printf("IT IS UP MOVE...");
break;
}
}
顺便说一下,type
是一个非常糟糕的名字,因为它感觉很像它应该是一个保留关键字。
我在头文件中有 enum
。
typedef enum{
up = 8, down = 2, left = 4, right = 6
}direction;
我想使用枚举来识别移动类型。 像这样:
void sayTypeOfMove(int type){
switch(type){
case direction.up:
printf("IT IS UP MOVE...");
break;
}
}
代码编译不通过,问题出在哪里?
定义如
typedef enum{
up = 8, down = 2, left = 4, right = 6
}direction;
direction
是一个 类型 ,它不是一个变量。
您需要定义一个该类型的变量,然后使用该值。
记住,枚举根本没有成员变量访问概念。枚举器列表包含 "enumeration-constant" s。你可以直接使用它们,作为一个值。
C 在知道您正在处理枚举时理解枚举元素,因此正确的代码是
void sayTypeOfMove(direction type){
switch(type){
case up:
printf("IT IS UP MOVE...");
break;
}
}
顺便说一下,type
是一个非常糟糕的名字,因为它感觉很像它应该是一个保留关键字。