Connecting Tech Pros Worldwide Help | Site Map

visual c++ : inside for loop scope declaration problem

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:12 PM
Christophe Grimault
Guest
 
Posts: n/a
Default 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



  #2  
Old July 19th, 2005, 07:12 PM
Michael Winter
Guest
 
Posts: n/a
Default Re: visual c++ : inside for loop scope declaration problem

"Christophe Grimault" wrote on 14 Oct 03:
[color=blue]
> 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[/color]
to[color=blue]
> 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 ?[/color]

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)


  #3  
Old July 19th, 2005, 07:12 PM
Peter van Merkerk
Guest
 
Posts: n/a
Default Re: visual c++ : inside for loop scope declaration problem

> for(int i = 0; i<10; i++)[color=blue]
> 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...[/color]

MSVC6 is wrong in this case.
[color=blue]
> Is there a compiler option to set for MSVC++6 to accept this ?[/color]

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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.