473,498 Members | 891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

visual c++ : inside for loop scope declaration problem

Hi all,

look at the following code:

//////////////////////////

for(int i = 0; i<10; i++)
cout << i;
for(int i = 0; i<20; i++)
cout << i;

//////////////////////////

It compiles and runs under gcc. However, when I give code like this to
somebody who tries to run it under MSVC++6, it seems complains about
redefinition of i, in the second loop...

Is there a compiler option to set for MSVC++6 to accept this ?

Thanks for any help.

CG
Jul 19 '05 #1
2 6050
"Christophe Grimault" wrote on 14 Oct 03:
Hi all,

look at the following code:

//////////////////////////

for(int i = 0; i<10; i++)
cout << i;
for(int i = 0; i<20; i++)
cout << i;

//////////////////////////

It compiles and runs under gcc. However, when I give code like this to somebody who tries to run it under MSVC++6, it seems complains about
redefinition of i, in the second loop...

Is there a compiler option to set for MSVC++6 to accept this ?


That behaviour is a Microsoft Extension to the C++ Standard. The only
way to allow what you've prevented is to disable (*ALL*) the language
extensions. This can be done in the project settings through either
the check-box, or by adding /Za anywhere to the switches list at the
bottom of most option pages. Be warned though: this could cause a lot
of problems if you do rely on some of the extensions, so it might be
easier to do a single declaration:

Read "Disable Language Extensions" for some more information.

Mike

P.S. This derives from KB article Q167748 which refers to VC++ 5.0. I
think it will apply, but if not, assume that there is no solutions
other than a single declaration.

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 19 '05 #2
> for(int i = 0; i<10; i++)
cout << i;
for(int i = 0; i<20; i++)
cout << i;

It compiles and runs under gcc. However, when I give code like this to
somebody who tries to run it under MSVC++6, it seems complains about
redefinition of i, in the second loop...
MSVC6 is wrong in this case.
Is there a compiler option to set for MSVC++6 to accept this ?


This newgroup is about standard C++, compiler specific options are off
topic here. You are more likely to get a good peer reviewed answer if
you ask this question in a newsgroup dedicated to that particular
compiler such as news:microsoft.public.vc. See also
http://www.slack.net/~shiva/welcome.txt

<OT>
IIRC disabling language extensions (/Za option) will correct the
scoping problem, but unfortunately causes a lot of problems with header
files. Another fix is to use this macro: #define for if(1)
for
</OT>

HTH

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #3

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

Similar topics

5
1892
by: Gunnar G | last post by:
Is there a problem with the declaration of the y-variable in the for loop? I'm not sure about the break, (if it should be there or not since I didn't write the original code), but the error:...
12
2692
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
1
5189
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
4
2678
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
28
4270
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
9
7501
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
37
2872
by: kumarchi | last post by:
hello: I recently compiled a numerically intensive c project under cygwin gcc 3.4.4 and microsoft visual c. The platform is intel T2400 1.83 ghz dual core lap top. the numerical stuff is both...
5
4032
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
1
3024
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
0
6998
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...
0
7163
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,...
0
7200
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...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
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...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
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...
0
3090
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.