[英]c++ Structure Arrays - “This declaration has no storage class or type specifier”
I'm having trouble with struct arrays. 我在使用结构数组时遇到了麻烦。 this is the code I have that produces 2 red error lines.
这是我产生2条红色错误线的代码。
struct frequents
{
int count;
char letter;
};
frequents testArray[2];
testArray[1].letter = 'v';
The error appears under testArray which has the declaration error and under '.' 该错误出现在具有声明错误的testArray下,并在“。”下。 which mentions that it expected a ';'.
其中提到它期望一个';'。
This is a statement : 这是一条声明 :
testArray[1].letter = 'v';
Statements are to be executed inside functions (function body) and not in some arbitrary global namespace as you have it now. 语句要在函数(函数体)中执行,而不要像现在那样在任意的全局命名空间中执行。 Move your statement inside a main program entry point function , a lambda , a free standing function or a class member function body.
在主程序入口点函数 , lambda ,独立函数或类成员函数体内移动您的语句。
Alternatively, use the aggregate initialization to initialize your array: 或者,使用聚合初始化来初始化数组:
frequents testArray[2] = {{ 1, 'a' }, { 2, 'b' }};
or without the extra braces: 或没有多余的花括号:
frequents testArray[2] = { 1, 'a', 2, 'b' };
Because you're not in a function: replace 由于您不在函数中:请替换
testArray[1].letter = 'v';
by 通过
int main() {
testArray[1].letter = 'v';
return 0;
}
and it compiles allright. 并且可以编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.