什么是“表达式必须具有类类型"?错误是什么意思?
问题描述:
#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
}
}
在这里我在做什么错,因为该计划只是用一些数字填充数组
What am I doing wrong here becuase the plan is to just fill the array with some numbers
答
执行以下操作:
int arrayTest[512];
int size = sizeof(arrayTest)/sizeof(*arrayTest);
C样式的数组没有成员函数.他们没有阶级的概念.
C-style arrays don't have member function. They don't have any notion of class.
无论如何,更好使用std::array
:
#include <array>
std::array<int,512> arrayTest;
int size = arrayTest.size(); //this line is exactly same as you wrote!
看起来像您想要的.现在,您可以使用索引i
作为arrayTest[i]
来访问arrayTest
的元素,其中i
的范围可以从0
到size-1
(包括).
which looks like what you wanted. Now you can use index i
to access elements of arrayTest
as arrayTest[i]
where i
can vary from 0
to size-1
(inclusive).