Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Initialization Error in VC++ 6

Status
Not open for further replies.

tas1326

Programmer
Jun 25, 2002
5
US
Does anyone know a way around the multiple initialization error caused by code similar to the following?

for(int i=0;i<10;i++)
.
.
.
for(int i=0;i<20;i++)

Throws the following error on compile:

error C2374: 'i' : redefinition; multiple initialization

Scoping rules would, by my understanding, dictate that this syntax is legal as long as the second for statement above was not nested inside the first such as:

for(int i=0;i<10;i++)
{
for(int i=0;i<20;i++)
}

Therfore the scope of i in the 1st example would only be for the life of the first for statement, it should then be able to be used again. However, VC++ 6 throws the error.

Now if you do the same thing dropping the type on the second for loop it works fine

for(int i=0;i<10;i++)
.
.
.
for(i=0;i<20;i++) //notice no type, using 1st reference

Or, and here is the kicker. If you do nest it, in which case the scoping should be an issue, shown below, it works fine.

for(int i=0;i<10;i++)
{
for(int i=0;i<20;i++)
}

Someone had mentioned to me that they thought this was a known issue? True? Someone please give me a sanity check, maybe I have been staring at the screen too long. If this is the case I am not a happy camper. We have some code based on an API that needs to compile under both x86 and UNIX. This code compiles fine under Sun Forte C++. I am not looking forward to crawling code and change for loop variable names.
 
This is a known issue with MSVC++. It is pretty stupid that they are not conforming to the standard which states that a variable declared like that should only have scope inside the for loop.
MS interprets

for (int i=0; i < 10; i++) {
// code
}

as

int i;
for (i=0; i < 10; i++) {
// code
}

If you wish to get around the problem (and make it work like it does on almost every other compiler) is to put braces around the for loop to specifically make it another code block:

{
for (int i=0; i < 10; i++) {
// code
}
}

If you do it like that it will work like in Unix, etc.

Also, the reason
for (int i ...) {
for (int i...) {
}
}
works in MSVC++ is because it thinks the first int i is outside the for loop and the second int i is inside the first for loop. This means that the second int i is in a more localized scope. BEWARE of this because with MSVC++, the second int i works in the scope of the outer for loop and the first int i will not.

I'm not sure if the last part was clearly stated, but it is true.

-Skatanic
 
Well, someone I worked with actually found the answer buried down in a listing of MS VC++ non standard functionality. There are 2 options to remedy this.

1. Under Project->Project Settings->C/C++->Customize click Disable Language Extensions (/Za switch). You will probably have issues using STL though.

2. Or place the following #define in your code.
#define for if(0);else for. Don't knwo exactly what it means but this is the workaround they suggest.

Looks like this has been an issue for coming up on 5 years now. Oh well, they will get to it someday, right. ;)
 
I never heard about the first fix option, but as for the second one...

It works in the same way as I suggested to put the for loop in another set of braces. It makes the for loop run under an if-else statement which is a seperate code block.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top