c++ - 为什么这个 C 数组初始化不能在 g++ 下编译?

标签 c++ c arrays initialization

大家好 我从 C 项目中获取了这段代码。我无法在 g++ 下以任何方式编译它。

我能用它做什么?

 enum EnumIndexID{
       ID_VALUE_A=2,
       ID_VALUE_B=2
 }

 struct newtype {
     enum MyEnumID eid;
     const char *name;
} table[] = {
     [ID_VALUE_A] = { MyEnumA, "ID_MSG_HeartbeatReq"},
     [ID_VALUE_B] = { MyEnumB, "ID_MSG_HeartbeatReq"},
};

最佳答案

你确定你的编译器支持Designated Initializer吗?语法?

g++ 没有。 从该超链接:

Standard C89 requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized.

In ISO C99 you can give the elements in any order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C89 mode as well. This extension is not implemented in GNU C++.

关于c++ - 为什么这个 C 数组初始化不能在 g++ 下编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4829104/

相关文章:

C++ asterisk 星号运算符

c++ - 尝试从指针数组 c++ 打印时出现奇怪的输出

c - 警告 : format '%d' expects type 'int' , 但参数 2 的类型为 'int (*)(int *)'

javascript - 如何将变量的值传递给 JavaScript 中的不同调用?

c - 重复,但仍然使用标准输出

c++ - 在表达式模板中嵌套子表达式

C++管道问题

c++ - 显示如何向 Opera/Firefox 4 等 Vista 窗框的玻璃部分添加按钮/菜单的示例

CodeLite 构建选项未突出显示 : can't compile and test code (Ubuntu 11. 10)

c - MagickWand for C,调用 DestroyPixelWands 时出错