C 中枚举的前向声明(不是 C++)
C 中枚举的前向声明对我不起作用。
我搜索了 Internet 和 Stack Overflow,但所有有关枚举器前向声明的问题都涉及 C++。在 C 中声明枚举器要做什么?
将它们放在每个文件的顶部(或标头中),以便文件中的所有函数都可以访问它们?
Forward declaration of enums in C does not work for me.
I searched the Internet and Stack Overflow, but all of the questions regarding forward declarations of enumerators refer to C++. What do you do for declaring enumerators in C?
Put them at the top of each file (or in a header), so that all functions in the file can access them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将它们放在标头中,以便所有需要它们的文件都可以访问标头并使用其中的声明。
使用以下选项编译时:
GCC 4.2.1 (在 Mac OS X 10.7.1 (Lion)) 接受 添加以下代码:
添加
-pedantic
并警告:
因此,您不应该尝试在 C 中使用枚举类型的前向声明;当不被迫迂腐时,GCC 允许它作为扩展。
Put them in a header so that all files that need them can access the header and use the declarations from it.
When compiled with the options:
GCC 4.2.1 (on Mac OS X 10.7.1 (Lion)) accepts the following code:
Add
-pedantic
and it warns:Thus, you are not supposed to try using forward declarations of enumerated types in C; GCC allows it as an extension when not forced to be pedantic.
您不能“前向声明”枚举,因为编译器不知道枚举的大小。 C 标准规定“每个枚举类型应与 char、有符号整数类型或无符号整数类型兼容。类型的选择是实现定义的,但应能够表示所有成员的值枚举”。
You can't "forward-declare" enums because the compiler won't know the size of the enum. The C standard says " Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration".
我来到这里遇到了同样的错误,但是这里没有提供关于代码/错误的太多信息。
我的 Makefile 标志是:
-Wall -Wextra -Werror -pedantic -std=c17
在我的标题中,我有以下
enum
:教程 此处 和 there
建议使用如下内容:
这会导致以下错误
通过使用解决:
不确定使用的是哪个 C 标准、代码或参考 OP,但我有点同意:这个相对简单的问题的大多数教程和解决方案都有点模糊。
I came here having the same error, but there is not really much information provided here on the code/error.
My Makefile-flags are:
-Wall -Wextra -Werror -pedantic -std=c17
In my header I have the following
enum
:The tutorials here and there
Would recommend to use something like this:
This results in the error stated by the OP.
Solved by using:
Not sure which C-Standard, Code or reference OP was using, but I somewhat agree: Most tutorials and solutions for this relatively simple issue are kinda ambiguous.
CoinResult 不是枚举;这是一种类型。如果你有
那
就是正确的。
CoinResult isn't an enum; it's a type. If you had
then
would be correct.