visual c++ : inside for loop scope declaration problem

  • Thread starter Christophe Grimault
  • Start date
C

Christophe Grimault

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
 
M

Michael Winter

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.
 
P

Peter van Merkerk

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top