Solved

Lifetime of loop variables

Posted on 2004-04-04
8
208 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 !
0
Comment
Question by:mrwad99
  • 4
  • 3
8 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10751504
in BCB this compiles OK
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10751510
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 :)
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10751563
This is a bug in MSVC++. I can't tell you how often I've cursed about that...
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10751565
khkremer; any MS docs for that online at all ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 20 total points
ID: 10751568
... you can however turn the standard behavior on by using the information from this page: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_c.2b2b_.for_statement.asp
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10751590
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 ?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10751610
Maybe this is only for VS.NET... at least it's under the .NET section.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10751615
Yeah prob; I am getting weird errors like

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

anyway.

Thanks for the help overall khkremer :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now