Public Group

# Dev-C++ error, "Expected unqualified-id before "for""

This topic is 4560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm using Dev-C++ and I recently ran accross this error message that I can't make heads or tails of. My code so far is:
int x=220;
int y=585;

for (int i=0;i<20;i++)
{
for(int h=0;h<10;h++)
{
myGrid.grid[h].x=x;
myGrid.grid[h].y=y;
x+=20;
}
x=220;
y+=20;
}


The full error message I'm getting is:
expected unqualified-id before "for"
expected ,' or ;' before "for"
expected constructor, destructor, or type conversion before '<' token
expected ,' or ;' before '<' token
expected constructor, destructor, or type conversion before '++' token
expected ,' or ;' before '++' token


It looks like I'm missing a semicolon somewhere but I'm sure I'm not. I've been over this code for a good hour, changing it and whatnot but I haven't been able to fix it. It'll probably just end up being a stupid mistake that I missed, but it's always easier to have someone else's eyes look at it.

##### Share on other sites
Uh, that *is* inside a function, right? You can't write "code" "at the top level" like that. You can only declare and initialize (not "assign to") globals, or write classes/functions/etc.

##### Share on other sites
Yeah, its inside a function. Maybe this will be one of those things where if you ignore it for a while it'll go away ;)

##### Share on other sites
Is this a .cpp file or a .c file? I can't remember for sure, since it's been a while since I programmed in C, but there is (or at least used to be) a restriction where you had to define your local variables at the top of a block of code. I'm not sure if defining them within the initializer part of a for-statement would count as top of the block or not.

Just wild speculation. Given just that code, though, I can't see anything that is obviously wrong, so wild speculation is all I have left. [smile]

One question: Is the error referring to the first for-statement, or the second? Also, did you copy-paste this code into GameDev, or re-type it? Sometimes small subtle mistakes can exist in the original, but when you re-type it to ask for help, you unknowingly fix the problem.

##### Share on other sites
I Found it!!!

I should've showed you guys the 10 lines before this also, you guys probably would have caught it. I had a 4 nested if statements above it and at the end of them I accidently put 5 }'s, and the compiler took the last one as the one closing the function, putting this code out in the open which, like Zahlman, pointed out, throws the error.

Thanks for the help guys.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015493
×