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

标签 c++ arrays

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

相关文章:

C++:如何更改标准输出的默认目标?

c++ - 从仅包含其迭代器的列表中删除元素

c - C中的数组指针和指针数组

c++ - 请求非类成员______

c++ - C++ 是否支持多类型变量?

c++ - 结合 GMPXX 和 C++11 及更高版本

Java数组栈,从上到下打印栈内容

c++ - C++中基于范围的for循环

c - 使用 strtok 将事物放入单独的数组中

c++ - 将 3D 数组传递给类(转换问题)