?
Solved

Lifetime of loop variables

Posted on 2004-04-04
8
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 80 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

650 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