473,785 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scope of for loop in VC6

When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable
redeclaration. I thought that the i stayed within the scope of the for loop?
The same code on gcc doesn't give this error (not on the default settings at
least). Is VC being fussy, and can I turn this fussiness off?

--
Nick H
Jul 22 '05 #1
7 2824
Nick Howes wrote:
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of
variable redeclaration. I thought that the i stayed within the scope
of the for loop? The same code on gcc doesn't give this error (not on
the default settings at least). Is VC being fussy, and can I turn
this fussiness off?


It's a VC6 bug. Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003
Pro edition optimizing compiler and standard library.

- Pete
Jul 22 '05 #2

"Pete C." <x@x.x> wrote in message
news:D9******** ********@newsre ad1.news.pas.ea rthlink.net...
Nick Howes wrote:
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of
variable redeclaration. I thought that the i stayed within the scope
of the for loop? The same code on gcc doesn't give this error (not on
the default settings at least). Is VC being fussy, and can I turn
this fussiness off?
It's a VC6 bug.


It's not really a bug, it's a relic of pre-standard C++.
Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003 Pro edition optimizing compiler and standard library.


If you don't want to switch compilers, an alternative is this macro. Place
it at the beginning of each file.

#define for if (0); else for

Jul 22 '05 #3
Nick Howes 2004-06-15 :
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable
redeclaration. I thought that the i stayed within the scope of the for loop?
The same code on gcc doesn't give this error (not on the default settings at
least). Is VC being fussy, and can I turn this fussiness off?


You are a bit off-topic here, but anyhow...
I don't know about VC6, but in VC .NET it's an option in project
properties -> C/C++ -> language. I don't know it's precise naming in
English since I have it in Italian, but it should be something like
"impose for cycle scope conformity".
By default it's turned off, which in my opinion is plain wrong.
I wish I knew a way to have it turned on by default for every new project
I start...

Walter Tross
Jul 22 '05 #4
"Walter Tross" <wa****@waltert ross.com> wrote in message
news:sw******** *************** ****@40tude.net ...
Nick Howes 2004-06-15 :
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable redeclaration. I thought that the i stayed within the scope of the for loop? The same code on gcc doesn't give this error (not on the default settings at least). Is VC being fussy, and can I turn this fussiness off?


You are a bit off-topic here, but anyhow...
I don't know about VC6, but in VC .NET it's an option in project
properties -> C/C++ -> language. I don't know it's precise naming in
English since I have it in Italian, but it should be something like
"impose for cycle scope conformity".


Sorry about the OT-ness, should've looked up a visual studio newsgroup.
Thanks everyone for your advice anyhow, there's no such option in VC6 but i
will look up that VC++ Toolkit 2003 and have a look.

Cheers
Nick H
Jul 22 '05 #5
Walter Tross wrote:

Nick Howes 2004-06-15 :
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable
redeclaration. I thought that the i stayed within the scope of the for loop?
The same code on gcc doesn't give this error (not on the default settings at
least). Is VC being fussy, and can I turn this fussiness off?


You are a bit off-topic here, but anyhow...

<snip>

au contraire --

This is very *much* on topic. Prior to the (pre)standard change to for-loop
scoping rules, variables declared in a for-loop were still in scope outside of
the loop.

This conformity issue, in part, is due to the very *long* time that it took for
the standard to actually be finalized/approved/ratified in the 90's.
Jul 22 '05 #6
"Nick Howes" <n.*******@warw ick.ac.uk> wrote in message news:<ca******* ***@wisteria.cs v.warwick.ac.uk >...

[ ... ]
Sorry about the OT-ness, should've looked up a visual studio newsgroup.
Thanks everyone for your advice anyhow, there's no such option in VC6 but i
will look up that VC++ Toolkit 2003 and have a look.


VC++ 6 has an option (/Za) that enforces the correct scope for
variables defined in the header of a for loop (etc.) The problem is
that when you turn this switch on, it enforces a number of other
rules, and its standard headers (among other things) won't compile
when those rules are enforced -- the result is that even though the
compiler itself has the capability, the remainder of the
implementation renders it unusable.
--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #7
Pete C. wrote:
Nick Howes wrote:
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of
variable redeclaration. I thought that the i stayed within the scope
of the for loop? The same code on gcc doesn't give this error (not on
the default settings at least). Is VC being fussy, and can I turn
this fussiness off?

It's a VC6 bug. Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003
Pro edition optimizing compiler and standard library.


I know we're getting OT here, but when you say "use that in place of the
VC6 compiler" do you mean that the v13 MS C++ compiler is compatible
with Visual Studio 6, i.e. interchangeable with the v12 compiler? I
wasn't aware of that; it would be very interesting to me if true.

--
Mike Smith

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
3554
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
6
2356
by: Arthur J. O'Dwyer | last post by:
I was paging through Coplien's book "Advanced C++ Programming Styles and Idioms" this afternoon and found some code that looked something like void sort(vector<foo> a) { int flip; do { for (int i=0, flip = 0; i < a.size(); ++i) {
32
3821
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
8
2559
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
3
2672
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the value of y (works well). create table test (a integer) @ create table debug1 (a integer) @ create trigger test_1 after insert on test referencing new as ins for
2
3196
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. Recently we (my fellow team members & I) observed an InvalidOperationException - 'collection has been modified', however it wasn't immediately obvious why this would be occuring. There are no modifications occuring within the loop and there is...
14
3559
by: Michael Moreno | last post by:
Hello, Would you know what is best practice please between say: CODE 1: TimeSpan ts; for (i=0; i<1000; i++) {
8
4533
by: Erik de Castro Lopo | last post by:
Hi all, Consider the following code snippet: do { int r = rand () ; } while (r != 0) ; It seems the compiler I'm using (GCC) does realise that the
9
7525
by: pauldepstein | last post by:
On my visual c++ compiler, I compiled code which contained something like for( int i =0; i < 5; i++) { double x =5;} I expected it to give a compiler error because x is being redefined
2
2900
by: ray | last post by:
Hi, all, foreach($array as $k =$v) { $foo = ...; } echo $foo; Is it allowed to access the $foo variable that is created within the loop from outside of the loop? I think it isn't allowed, because according to the rule stated in PHP manual(http://www.php.net/manual/ en/language.variables.scope.php): The scope of a variable is the
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.