error: expected unqualified-id before ‘for’

Viewed 77355

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;
}
3 Answers

Your for loop is outside a function body.

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;
}
Related