c++ - 使用索引指示符初始化 _C++_ 对象数组

标签 c++

我正在尝试将一些代码移植到 GCC,它可以使用 IAR 编译器编译。该代码初始化一个 C++ 对象数组(一个包含字符数组的结构)。我可以让它在 C 中与 GCC 一起工作,但不能与 C++ 一起工作。这是一个简单的例子。

#include  <stdio.h>

typedef struct
{
 int lineID[10];
} TMenu;

static const TMenu t1[8] =
{
    {{3}},
    {{4}},
    [6] = {{33, 22}},
    [8] = {{33, 22}},
    {{}},
    {{9,8,7,6,5,4,3,2,1}},
};

注意:我还必须在初始化器周围添加额外的大括号,IAR 对此没有提示。

它用 GCC 编译很好,但是当用 G++ 编译时,我得到以下错误。

x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '._2 = {{33, 22}}'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '._3 = {{33, 22}}'

最佳答案

看起来 GCC 4.7 越来越接近于支持这种结构。这是 GCC 4.5、4.6 和 4.7 编译同一示例时的输出。

海湾合作委员会 4.5.3

$ /opt/local/bin/g++-mp-4.5 -Wall -std=c++0x -o y.exe x.c
x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '._2 = {{33, 22}}'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '._3 = {{33, 22}}'

海湾合作委员会 4.6.3

$ /opt/local/bin/g++-mp-4.6 -Wall -std=c++0x -o y.exe x.c
x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '{} = {{33, 22}}'
x.c:12:20: note: candidate is:
x.c:12:7: note: <lambda()>&<lambda()>::operator=(const<lambda()>&) <deleted>
x.c:12:7: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const<lambda()>&'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '{} = {{33, 22}}'
x.c:13:20: note: candidate is:
x.c:13:7: note: <lambda()>&<lambda()>::operator=(const<lambda()>&) <deleted>
x.c:13:7: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const<lambda()>&'

海湾合作委员会 4.7.0

$ /opt/local/bin/g++-mp-4.7 -Wall -std=c++0x -o y.exe x.c
x.c:16:1: sorry, unimplemented: non-trivial designated initializers not supported
x.c:16:1: sorry, unimplemented: non-trivial designated initializers not supported

关于c++ - 使用索引指示符初始化 _C++_ 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986703/

相关文章:

c++ - C++ 中的继承不起作用。编译以下 C++ 代码时发生链接器错误

c++ - 如何从点云与任意平面的交点获取曲线?

c++ - 使用 C++ 类的体系结构 x86_64 的 undefined symbol

c++ - 从函数返回本地 var 字符串失败,而本地 var int 和 char 工作正常。为什么?

c++ - Win32,等待主消息队列中的线程?

c++ - 是否可以通过#define 将 g++ 设置为遵循 C++11 ISO (-std=c++11)?

c++ - 为什么输出运算符是 'os << value' 而不是 'value >> os' ?

c++ - : non-blocking write, 假想的锁机制读取并失效

c++ - 无法处理信号,导致系统调用中断

c++ - 如何创建两个具有重载构造函数的动态对象?