C++ “表达式必须具有类类型”错误是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18965212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:22:20  来源:igfitidea点击:

What does "expression must have class type" error mean?

c++arrays

提问by Robert Spratlin

#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

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

回答by Nawaz

Do this:

做这个:

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

C-style arrays don't have member function. They don't have any notion of class.

C 风格的数组没有成员函数。他们没有阶级观念。

Anyway, betteruse std::array:

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

#include <array>

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

which looks like what you wanted. Now you can use index ito access elements of arrayTestas arrayTest[i]where ican vary from 0to size-1(inclusive).

这看起来像你想要的。现在您可以使用 indexi来访问arrayTestas arrayTest[i]where 的元素,i可以从0size-1(包括)变化。

回答by Shafik Yaghmour

arrayTestis not a class or struct but an array and it does not have member functions, in this case this will get your the size of the array:

arrayTest不是一个类或结构,而是一个数组,它没有成员函数,在这种情况下,这将获得数组的大小:

size_t size = sizeof(arrayTest)/sizeof(int);

although if your compiler supports C++11than using std::arraywould be better:

尽管如果您的编译器支持C++11,那么使用std::array会更好:

#include <array>

std::array<int,512> arrayTest ;
size_t size = arrayTest.size() ;

as the document linked above shows you can also use range for loop to iterate over the elements of a std::array:

正如上面链接的文档所示,您还可以使用 range for 循环来迭代std::array的元素:

for( auto &elem : arrayTest )
{
   //Some operation here
}

回答by Dark Falcon

Arrays don't have members. You have to use something like:

数组没有成员。你必须使用类似的东西:

int size = sizeof(arrayTest) / sizeof(arrayTest[0]);

Better yet, if you must work with normal arrays instead of std::array, use a helper function. This also has the advantage of not breaking when you attempt it on a pointer instead of an array:

更好的是,如果您必须使用普通数组而不是std::array,请使用辅助函数。这还具有在您尝试使用指针而不是数组时不会中断的优点:

template<int N, typename T> int array_size(T (&)[N]) {return N;}

int size = array_size(arrayTest);

回答by jimifiki

If you are stuck with arrays you can define your getArraySize function:

如果你被数组卡住了,你可以定义你的 getArraySize 函数:

template <typename T,unsigned S> 
inline unsigned getArraySize(const T (&v)[S]) { return S; } 

seen here: http://www.cplusplus.com/forum/general/33669/#msg181103

在这里看到:http: //www.cplusplus.com/forum/general/33669/#msg181103

std::array remains the better solution.

std::array 仍然是更好的解决方案。