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

标签 c visual-studio-2003

我讨厌定义。为了从旧代码库中尽可能多地消除它们,我需要依赖 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/

相关文章:

c - 为随机全局变量播种 rand()

windows - 在 Windows 7 64 位上安装 Visual Studio 2003

c++ - MS 对 STL 的扩展

c - scanf 把文件指针放在哪里?

c++ - c++ 数组上使用的 delete[] 运算符的理解问题

iphone - 读取以单斜杠或双斜杠开头的文件路径的区别

c++ - `POLLOUT` Linux函数中的 `poll`事件是什么意思?

c++ - 如何禁用 C++ 7.1 名称修改? :(

c++ - 父类和内部类同名时名称解析错误

.net - 如何使 .NET 可执行文件在 64 位操作系统上作为 32 位进程运行?