gpt4 book ai didi

c++ - "expression must have class type"错误是什么意思?

转载 作者:太空狗 更新时间:2023-10-29 19:39:25 25 4
gpt4 key购买 nike

#include <cstdlib>
using namespace std;

int main()
{
int arrayTest[512];
int size = arrayTest.size();
for(int a = 0;a<size;a++)
{
//stuff will go here
}
}

我在这里做错了什么,因为计划只是用一些数字填充数组

最佳答案

这样做:

int arrayTest[512];
int size = sizeof(arrayTest)/sizeof(*arrayTest);

C 风格数组没有成员函数。他们没有任何类(Class)观念。

无论如何,更好使用std::array:

#include <array>

std::array<int,512> arrayTest;
int size = arrayTest.size(); //this line is exactly same as you wrote!

这看起来像你想要的。现在您可以使用索引 i 访问 arrayTest 的元素作为 arrayTest[i] 其中 i 可以不同于 0size-1(含)。

关于c++ - "expression must have class type"错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18965212/

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