我不确定使用C枚举的正确语法是什么。我有以下代码:
enum {RANDOM, IMMEDIATE, SEARCH} strategy;
strategy = IMMEDIATE;
但是这不能编译,会出现以下错误:
error: conflicting types for ‘strategy’
error: previous declaration of ‘strategy’ was here
我做错了什么?
我不确定使用C枚举的正确语法是什么。我有以下代码:
enum {RANDOM, IMMEDIATE, SEARCH} strategy;
strategy = IMMEDIATE;
但是这不能编译,会出现以下错误:
error: conflicting types for ‘strategy’
error: previous declaration of ‘strategy’ was here
我做错了什么?
当前回答
值得指出的是,您不需要类型定义。你可以像下面这样做
enum strategy { RANDOM, IMMEDIATE, SEARCH };
enum strategy my_strategy = IMMEDIATE;
这是一个风格问题,你是否喜欢typedef。如果没有它,如果要引用枚举类型,则需要使用枚举策略。有了它,你可以说策略。
两种方法都有优缺点。一种方法比较冗长,但将类型标识符保留在标记名称空间中,在那里它们不会与普通标识符冲突(想想struct stat和stat函数:它们也不冲突),并且在那里您立即看到它是一个类型。另一个较短,但将类型标识符引入普通名称空间。
其他回答
我最喜欢和唯一使用的结构是:
typedef enum MyBestEnum
{
/* good enough */
GOOD = 0,
/* even better */
BETTER,
/* divine */
BEST
};
我相信这会解决你的问题。在我看来,使用新字体是正确的选择。
我尝试了gcc,并提出了我的需要,我被迫使用最后一种选择,编译出来的错误。
类型定义enum状态{a = 0, b = 1, c = 2}状态;
typedef enum state {a = 0, b = 1, c = 2} state;
typedef enum state old; // New type, alias of the state type.
typedef enum state new; // New type, alias of the state type.
new now = a;
old before = b;
printf("State now = %d \n", now);
printf("Sate before = %d \n\n", before);
@ThoAppelsin in his comment to question posted is right. The code snippet posted in the question it is valid and with no errors. The error you have must be because other bad syntax in any other place of your c source file. enum{a,b,c}; defines three symbolic constants (a, b and c) which are integers with values 0,1 and 2 respectively, but when we use enum it is because we don't usually care about the specific integer value, we care more about the meaning of the symbolic constant name. This means you can have this:
#include <stdio.h>
enum {a,b,c};
int main(){
printf("%d\n",b);
return 0;
}
这个输出是1。
这也是有效的:
#include <stdio.h>
enum {a,b,c};
int bb=b;
int main(){
printf("%d\n",bb);
return 0;
}
并将输出与之前相同的结果。
如果你这样做:
enum {a,b,c};
enum {a,b,c};
你会有一个错误,但是如果你这样做:
enum alfa{a,b,c};
enum alfa;
你不会有任何错误。
你可以这样做:
enum {a,b,c};
int aa=a;
aa是一个值为0的整型变量。但你也可以这样做:
enum {a,b,c} aa= a;
和将具有相同的效果(即aa是一个值为0的int)。
你还可以这样做:
enum {a,b,c} aa= a;
aa= 7;
aa将是int,值为7。
因为你不能使用enum重复符号常量定义,就像我之前说过的,如果你想使用enum声明int变量,你必须使用标签:
enum tag1 {a,b,c};
enum tag1 var1= a;
enum tag1 var2= b;
typedef的使用是为了保证你不用每次都用枚举tag1来定义变量。使用typedef,你可以输入Tag1:
typedef enum {a,b,c} Tag1;
Tag1 var1= a;
Tag1 var2= b;
你还可以有:
typedef enum tag1{a,b,c}Tag1;
Tag1 var1= a;
enum tag1 var2= b;
最后要说的是,因为我们谈论的是定义的符号常量,所以在使用enum时最好使用大写字母,例如:
enum {A,B,C};
而不是
enum {a,b,c};
你尝试两次声明策略,这就是为什么你会得到上面的错误。以下工作没有任何抱怨(编译gcc -ansi - petic -Wall):
#include <stdio.h>
enum { RANDOM, IMMEDIATE, SEARCH } strategy = IMMEDIATE;
int main(int argc, char** argv){
printf("strategy: %d\n", strategy);
return 0;
}
如果将第二行改为:
...
enum { RANDOM, IMMEDIATE, SEARCH } strategy;
strategy = IMMEDIATE;
...
从警告中,你可以很容易地发现你的错误:
enums.c:5:1: warning: data definition has no type or storage class [enabled by default]
enums.c:5:1: warning: type defaults to ‘int’ in declaration of ‘strategy’ [-Wimplicit-int]
enums.c:5:1: error: conflicting types for ‘strategy’
enums.c:4:36: note: previous declaration of ‘strategy’ was here
因此,编译器使用strategy = IMMEDIATE来声明一个默认类型为int的名为strategy的变量,但之前已经声明了一个具有此名称的变量。
然而,如果你把赋值放在main()函数中,它将是一个有效的代码:
#include <stdio.h>
enum { RANDOM, IMMEDIATE, SEARCH } strategy = IMMEDIATE;
int main(int argc, char** argv){
strategy=SEARCH;
printf("strategy: %d\n", strategy);
return 0;
}
值得指出的是,您不需要类型定义。你可以像下面这样做
enum strategy { RANDOM, IMMEDIATE, SEARCH };
enum strategy my_strategy = IMMEDIATE;
这是一个风格问题,你是否喜欢typedef。如果没有它,如果要引用枚举类型,则需要使用枚举策略。有了它,你可以说策略。
两种方法都有优缺点。一种方法比较冗长,但将类型标识符保留在标记名称空间中,在那里它们不会与普通标识符冲突(想想struct stat和stat函数:它们也不冲突),并且在那里您立即看到它是一个类型。另一个较短,但将类型标识符引入普通名称空间。