I have a very simple bit of code that won't work, and have no idea why
The following:
int flag = 0;
if (flag == 0)
{
flag = 1;
}
Wil开发者_运维技巧l not compile. It is already a quite complex program, and I am able to do other actions within the program with no problems at all, yet for some reason I can't reference a variable I have just created. The variable name is unique, and the application is a Windows app including windows.h. It is written in C, and up until now I have not attempted to create my own variables.
I can't publish the full code here, least of all because there's pages of it, but can anyone speculate as to why it can't compile? I am using Visual C++ and have the following errors:
syntax error : missing ';' before 'type' (this applies to line 1)
'flag' : undeclared identifier (line 2)
'flag' : undeclared identifier (line 4)
I have tried using bool as well, with 'true' and 'false' in place. I am relatively new to C++. Note that the code compiles fine without it here at all. It comes immediately after a previous action within a larger 'if' statement, of which this is a part. I have successfully added other nested if statements in the exact same place as this. Taking it outside the if statement entirely makes no difference. Putting it all right at the top of my main.c file, just after the #includes, makes no difference. Removing it completely means the program compiles absolutely fine. The problem seems to lie with defining the variable.
I assume you compile it to C
language. You need to declare the variable at the beginning of the block:
// beginning of block
int flag=0;
//Some code
if (flag == 0)
{
flag = 1;
}
are you missing a ; at the end of the preceding line?
Looks to me, as if the line before the variable declaration has not been finished with a ;
. Check this line for the missing semicolon.
精彩评论