C 中枚举的前向声明(不是 C++)

发布于 2024-12-05 08:14:28 字数 146 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒岛晴空 2024-12-12 08:14:28

将它们放在标头中,以便所有需要它们的文件都可以访问标头并使用其中的声明。

使用以下选项编译时:

$ /usr/bin/gcc -g -std=c99 -Wall -Wextra -c enum.c

GCC 4.2.1 (在 Mac OS X 10.7.1 (Lion)) 接受 添加以下代码:

enum xyz;

struct qqq { enum xyz *p; };

enum xyz { abc, def, ghi, jkl };

添加 -pedantic并警告:

$ /usr/bin/gcc -g -std=c99 -Wall -Wextra -pedantic -c enum.c
enum.c:1: warning: ISO C forbids forward references to ‘enum’ types
enum.c:5: warning: ISO C forbids forward references to ‘enum’ types

因此,您不应该尝试在 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:

$ /usr/bin/gcc -g -std=c99 -Wall -Wextra -c enum.c

GCC 4.2.1 (on Mac OS X 10.7.1 (Lion)) accepts the following code:

enum xyz;

struct qqq { enum xyz *p; };

enum xyz { abc, def, ghi, jkl };

Add -pedantic and it warns:

$ /usr/bin/gcc -g -std=c99 -Wall -Wextra -pedantic -c enum.c
enum.c:1: warning: ISO C forbids forward references to ‘enum’ types
enum.c:5: warning: ISO C forbids forward references to ‘enum’ types

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.

横笛休吹塞上声 2024-12-12 08:14:28

您不能“前向声明”枚举,因为编译器不知道枚举的大小。 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".

流年已逝 2024-12-12 08:14:28

我来到这里遇到了同样的错误,但是这里没有提供关于代码/错误的太多信息。

我的 Makefile 标志是: -Wall -Wextra -Werror -pedantic -std=c17

在我的标题中,我有以下 enum

typedef enum 
{
  IS_HEAD = 1, 
  IS_VALUE = 2,
  IS_SIDE
} CoinResult;

教程 此处there

建议使用如下内容:

enum CoinResult cr;
cr = IS_SIDE;

这会导致以下错误

通过使用解决:

CoinResult cr = IS_SIDE; 

不确定使用的是哪个 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:

typedef enum 
{
  IS_HEAD = 1, 
  IS_VALUE = 2,
  IS_SIDE
} CoinResult;

The tutorials here and there

Would recommend to use something like this:

enum CoinResult cr;
cr = IS_SIDE;

This results in the error stated by the OP.

Solved by using:

CoinResult cr = IS_SIDE; 

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.

晚雾 2024-12-12 08:14:28

CoinResult 不是枚举;这是一种类型。如果你有

enum CoinResult {
    IS_HEAD = 1,
    IS_VALUE = 2,
    IS_SIDE,
};

    enum CoinResult cr;

就是正确的。

CoinResult isn't an enum; it's a type. If you had

enum CoinResult {
    IS_HEAD = 1,
    IS_VALUE = 2,
    IS_SIDE,
};

then

    enum CoinResult cr;

would be correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文