我正在尝试将一些代码移植到 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/