2

I am converting an old VC6 project in VS2008 and for some reason 2008 treats int i as undeclared if it is declared in a for().

In the code they do this many times:

for(int i = 0; ... ; i++)

...

for(i = 0; ... ; i++)

if(i = RANDOM_NUMBER)

It gives me int i is not declared errors, so I have to put int i = 0 at the top of every method. Is there a way around this? I'm not very familiar with C++.

ppl
  • 1,120
  • 5
  • 18
Synaps3
  • 1,597
  • 2
  • 15
  • 23

5 Answers5

4

If you just want to continue using the code without fixing it, you can give Microsoft's current compilers the -Zc:forScope- switch to force the compiler to follow the rules that were the norm a few decades ago (or so).

Obviously, I can't guarantee that their compilers will continue to support that indefinitely, so I'd still consider updating this code a fairly high priority, but the compiler switch will let you avoid it for now, and assign a priority rather than requiring that it be fixed immediately to continue using the code at all.

Interesting aside: the compiler in VC++ 6 was actually capable of following the current rule (but didn't by default). The switch to enforce the correct scope was unusable in practice though, because many (most?) of the the standard headers included with the compiler depended on non-standard behavior that was disabled with that switch.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

This is an annoying VC6 bug. If you have a lot of code and prefer not to convert/correct you may opt to use special compiler options to retain the old an incorrect behavior.

See /Ze article on msdn.

As pointed out in other answers, this behavior is non-standard compliant.

ppl
  • 1,120
  • 5
  • 18
0

In the old days of C++, an int declared in the for-loop would be defined in the block enclosing the loop. With the introduction of the C++ standard the scope of variables declared inside the loop, including the initialization part of the for-loop are restricted to the loop (the same goes for if, while, and switch statements).

There are two ways to address the problem:

  1. If you need to retain the value of the variable, you'd declare it prior to the for-statement:

    int i = 0;
    for (; ...; ++i) { ... }
    ...
    for (; ...; ++i) { ... }
    
  2. Otherwise, you'd just declare it again:

    for (int i = 0; ...; ++i) { ... }
    ...
    for (int i = 0; ...; ++i) { ... }
    

Also, I strongly recommend that you get an up to date C++ tutorial!

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
-1

Variables declared withing block will be no more accessible outside block.

{
    int i = 0;... 
} <-- i will not be accessesible outside this }

Simliarly you for loop is one block, hence int i declared in that for loop will not be accessible outside that.

for(int i=0;..;..)
{
} //i will not be accessible after this.

But if you are declaring it above it, its scope will be different and you can access it in may fors withing that scope.

int i=0;
for(i=0;..;..) <-- First for
{
}

for (i=0;..;..) <- Second for
{
}
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
-2

ANSI C forbids mixing declaration and statement, you need to use C99 standard for it. Unfortunately MS VS2008 does not support C99, so you could try another compiler (Intel for example) or another version of VS ( I heard VS2013 do support C99 standard but not sure).

vershov
  • 928
  • 4
  • 6
  • 1
    His question is about C++, not C. While the statements it contains are correct in themselves, your answer seems irrelevant to the question at hand. – Jerry Coffin Jan 13 '14 at 04:57
  • Author's question is about exact problem, not about languages. The problem is that VS uses C (not C++) compiler to process mentioned code. Obviously the problem will be gone in case of using C++ compiler. – vershov Jan 13 '14 at 05:03
  • 1
    Sorry, but no. The problem is that VC++ 6 (when compiling C++) followed a rule that was obsolete even when it was published (15+ years ago). At least by default, their compilers follow the current rule (again, when compiling C++). The attempt to use `for (int i ...` is not allowed in C89 at all, so the fix he's been using won't work either. – Jerry Coffin Jan 13 '14 at 05:06
  • C99 has the same scope as C++ for variables declared in a conditional. Switching to a C99 compiler will not make this program compile, even if it coincidentally uses no C++ features. – Casey Jan 13 '14 at 08:50