Solved

Lifetime of loop variables

Posted on 2004-04-04
8
209 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 19

Author Comment

by:mrwad99
ID: 10751565
khkremer; any MS docs for that online at all ?
0
 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

809 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