C++ 错误:'for' 之前的预期不合格 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2293970/
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:56:01  来源:igfitidea点击:

error: expected unqualified-id before ‘for’

c++

提问by Morlock

The following code returns this: error: expected unqualified-id before ‘for'

以下代码返回: error: expected unqualified-id before ‘for'

I can't find what is causing the error. Thanks for the help!

我找不到导致错误的原因。谢谢您的帮助!

#include<iostream>

using namespace std;

const int num_months = 12;

struct month {
    string name;
    int n_days;
};

month *months = new month [num_months];

string m[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int n[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

for (int i=0; i<num_months; i++) {
    // will initialize the months
}

int main() {
    // will print name[i]: days[i]
    return 0;
}

回答by missingfaktor

Your forloop is outside a function body.

您的for循环在函数体之外。

回答by Anthony Flores

Ok just to make this answer clear (since I made the rookie mistake too).

好的,只是为了明确这个答案(因为我也犯了新手错误)。

the for loop was outside int main() along with everything else since main() sits by itself empty at the bottom of the code.

for 循环与其他所有内容一起位于 int main() 之外,因为 main() 本身位于代码底部是空的。

Sorry more than needed to be said for some but since this issue is more directed to newbies a more elaborate explanation is needed.

抱歉,有些人需要多说几句,但由于这个问题更针对新手,因此需要更详细的解释。