Solved

Lifetime of loop variables

Posted on 2004-04-04
8
207 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now