gpt4 book ai didi

c++ - 类内部的数组初始化器

转载 作者:行者123 更新时间:2023-12-02 10:27:39 24 4
gpt4 key购买 nike

在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com