gpt4 book ai didi

带有 typedef 的结构中的 C 枚举定义

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:39 26 4
gpt4 key购买 nike

我发现结构中枚举的定义存在一些问题,我想要这样的东西:

typedef struct
{
typedef enum { E1, E2, E3 } E;
E e;
} S;

在 VS2012 中我有错误:

error C2071: 'E' : illegal storage class
error C2061: syntax error : identifier 'E'
error C2059: syntax error : '}'

我找到了 C2071 的解释,但不是同一种情况: http://msdn.microsoft.com/en-us/library/deb3kh5w.aspx

gcc-4.9 说:

error: expected specifier-qualifier-list before ‘typedef’

有趣的是代码:

typedef enum { E1, E2, E3 } E;
E e;

在全局范围和函数体中工作正常。

我也试过不使用 typedef 来做,但不幸的是仍然有很多错误:

error C2011: 'E' : 'enum' type redefinition
see declaration of 'E'
error C2208: 'E' : no members defined using this type

我发现了类似的原因:http://msdn.microsoft.com/en-us/library/ms927163.aspx但我确实定义了该类型的成员。

最佳答案

你应该像这样声明你的enum成员:

typedef struct
{
enum { E1, E2, E3 } e;
} S;

然后你可以这样做:

int main(void)
{
S s;
s.e = E1;

/* And so on */
}

关于带有 typedef 的结构中的 C 枚举定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27158571/

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