使用索引指示符初始化_C++_对象的数组
Initializing an array of _C++_ objects with index designators
我正在尝试将一些代码移植到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的输出。
GCC 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}}'
GCC 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()>&'
GCC 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
我从未见过像这样的语法
[6] = {33, 22}},
在C++中。您可以尝试只使用空的初始值设定项来填补空白。以下关于ideone的作品:
#include <iostream>
struct TMenu
{
int lineID[10];
};
int main(int, char*[])
{
const TMenu t1[8] =
{
{{3}},
{{4}},
{{33, 22}},
{{33, 22}},
{{}},
{{9, 8, 7, 6, 5, 4, 3, 2, 1}},
};
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 10; ++j)
{
std::cout << t1[i].lineID[j] << "t";
}
std::cout << std::endl;
}
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 使用"std::unordereded_map"映射到"std::list"对象