在arduino项目上工作时,我创建了一些无法编译的C++代码。
了解了它的终止点后,其实质如下:
const int xx[]={12,5};
void setup() {
// put your setup code here, to run once:
const int yy[]={12,5};
}
class aaaa{
//compilation stops here giving error too many intialiazers for 'const int[0]'
const int zz[]={12,5};
};
void loop() {
// put your main code here, to run repeatedly:
}
问题是为什么它不会编译,并给出错误“对于'const int [0]'的初始化太多。它应该表现出来吗?
最佳答案
您的第一个数组声明了一个全局变量,该变量的大小由初始化程序定义。
您的第二个数组是一个局部变量,再次允许它具有由初始化程序定义的大小。
您的第三个数组是结构的成员,结构成员必须具有固定的大小,以便结构本身具有固定的大小。不允许从初始化程序中推断出大小。
Clang给出了更清晰的错误消息:
error: array bound cannot be deduced from an in-class initializer
关于c++ - 类内部的数组初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63619336/