We help IT Professionals succeed at work.

Lifetime of loop variables

mrwad99
mrwad99 asked
on
Medium Priority
234 Views
Last Modified: 2010-04-01
Hello,

Easy one this.  If I have

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

and then later on

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

why does MSVC++ moan stating that there is a redefinition of 'i' ?  I thought that variables declared in loops like this only lasted for the period of the loop !  At least that is what my books say !  Or is this just a 'feature' of MSVC++ ?

TIA !
Comment
Watch Question

Commented:
in BCB this compiles OK

Author

Commented:
Right it looks like this is a 'feature' of MSVC++ then.  My exact error is

'i' : redefinition; multiple initialization
see declaration of 'i'

Right, I may have the answer then, but lets wait and see if any one else has feedback.

Thanks for testing that out mokule :)
CERTIFIED EXPERT

Commented:
This is a bug in MSVC++. I can't tell you how often I've cursed about that...

Author

Commented:
khkremer; any MS docs for that online at all ?
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hmm... I follow the info at that link and it gives two options, one is to "Disable Language Extensions" which works, the other is a switch /Zc:forScope which I cannot get to work.  

Can you ?
CERTIFIED EXPERT

Commented:
Maybe this is only for VS.NET... at least it's under the .NET section.

Author

Commented:
Yeah prob; I am getting weird errors like

Command line warning D4002 : ignoring unknown option '/Zc'

anyway.

Thanks for the help overall khkremer :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.