Skip to main content
gamedev.net
🔒 Locked

for loop, strange issue

Started by Arcibald Wearlot 4 replies 600+ views
Original Post
Arcibald Wearlot
Arcibald Wearlot
the problem is that this piece of code compiles under msvc++ 6.0, but it shouldnt:
	for(int i=0;i<10;i++)
	{
		//do stuff
	}
	for(i=0;i<10;i++)
	{
		//do other stuff
	}
 
if i remember well, according to ANSI c++ standard the variable i cannot be used outside the for loop. but if i use
	for(int i=0;i<10;i++)
	{}
	for(int i=0;i<10;i++)
	{}
 
i get this compiler error: error C2374: ''i'' : redefinition; multiple initialization is this a msvc++ fault? anyone getting the same error?
nonpop
nonpop
Yep, it''s a problem with the VC6 standard compliance
hplus0603
hplus0603
Put this in a header you include everywhere:

#define for if(0);else for

That will make VC++ 6.0 use correct for scoping, through macro magic.

In VC++ 7.0 and up (".NET") there is a C++ language checkbox to use conformant for scoping, but it''s not on by default -- you have to turn it on in your project.
enum Bool { True, False, FileNotFound };
Anon Mike
Anon Mike
As I recall the standard on this flip-flopped a couple times. When they finally got around to making up thier minds VC6 was to far into the ship cycle to change. VC7 logically put in a switch. The default is probably the non-standard behavior so 10 billion lines of code that already exist don''t break.
-Mike
_the_phantom_
_the_phantom_
correct, default on VC7.1 is non-conforming for reasons you stated, first thing i do on a project is flip it across to the correct way of doing things

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.