gpt4 book ai didi

c - 枚举 hack 在 C 中有效吗?如果是这样,它应该在 VisualStudio 2003 中工作吗?

转载 作者:太空狗 更新时间:2023-10-29 15:20:00 25 4
gpt4 key购买 nike

我讨厌定义。为了从旧代码库中尽可能多地消除它们,我需要依赖 enum-hack用于定义包含数组的结构。不幸的是,当 C 项目包含有罪的 .h 文件时,我无法编译它。

typedef struct _P_O {
enum { MAXELEMENTS=10 };
P_O_U elem;
ULONG id[MAXELEMENTS];
USHORT nid;
} P_O, *PP_O;

这会产生错误 C2208 :

'type' : no members defined using this type
An identifier resolving to a type name is in an aggregate declaration, but the compiler cannot declare a member.

OT:我讨厌使用 10 岁的编译器,不得不维护旧的 C 代码和糟糕的设计;不只是定义 :)

最佳答案

鉴于问题中的代码,GCC 警告:warning: declaration does not declare anything for the enum line.

将枚举放在结构之外就可以了。

typedef int P_O_U;
typedef unsigned long ULONG;
typedef unsigned short USHORT;

enum { MAXELEMENTS = 10 };

typedef struct _P_O
{
P_O_U elem;
ULONG id[MAXELEMENTS];
USHORT nid;
} P_O, *PP_O;

关于c - 枚举 hack 在 C 中有效吗?如果是这样,它应该在 VisualStudio 2003 中工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17997845/

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