开发者

error: expected unqualified-id before ‘for’

开发者 https://www.devze.com 2022-12-20 11:47 出处:网络
The following code returns this: error: expected unqualified-id before ‘for’ I can\'t find what is causing the error. Thanks for the help!

The following code returns this: 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;
}


Your for loop is outside a function body.


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

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

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


This is not directly related but might be helpful to someone finding this. I got a similar error error: expected unqualified-id before 'continue'

My problem was that I wrote std::continue like this:

std::vector<int> a = {1, 2, 3, 4, 5};
for(int i: a)
{
    if(i == 3)std::continue;
}

instead of just continue:

std::vector<int> a = {1, 2, 3, 4, 5};
for(int i: a)
{
    if(i == 3)continue;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消