问题描述
int code[128] = {['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15};
Exit code: 1 Errors while compiling:
10:72: sorry, unimplemented: non-trivial designated initializers not supported
['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15};
^
10:72: sorry, unimplemented: non-trivial designated initializers not supported
原因分析
编译时出现错误:抱歉,未实现:不支持非平凡的指定初始值设定项
这个错误信息表明,代码试图使用C99标准中的一种特性,即指定初始化器(designated initializers)。这是C99引入的一个特性,允许在初始化数组或结构体时指定某些特定元素或成员的值。这在C99和后续的C标准中是合法的,但在C++中并不支持。
解决方案
创建一个数组,然后分别设置对应的元素的值。
int code[128];
code['A'] = 10;
code['B'] = 11;
code['C'] = 12;
code['D'] = 13;
code['E'] = 14;
code['F'] = 15;