Archived

This topic is now archived and is closed to further replies.

airjy

for and visual c++ 6.0

Recommended Posts

airjy    122
today i started programming under windows with visual c++ 6.0 Before, i''ve only used borland or g++ under linux, and i''ve found something strange. if i do : for(int i=0;i<10;i++) { ... } i can still access i after the } and i cannot declare int i after that for.. that''s annoying because if i have to make another for just after, i have to use for(i=0:i<10;i++) {...} That code can''t be compiled with g++ under linux, and if i use int i in the 2nd for, visual c++ won''t compile with the following error. error C2374: ''i'' : redefinition; multiple initialization any ideas why visual c++ act so strangely? i haven''t touched the compilations options, they are the ones by default when creating an empty project.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I dont understand what the problem with that is, why not just say.

int i;
for(i=0;i<10;i++)
{
...

}
for(i=0;i<10;i++)
{
...

}
or


for(int i=0;i<10;i++)
{
...

}
for(int j=0;j<10;j++)
{
...

}

???

Share this post


Link to post
Share on other sites
airjy    122
my problem is that the scope for a variable is between { and }
maybe i''m wrong but i''ve always been told that
if i do
for(int i=0;i<10;i++)
{
...
}

//there the variable i doesn''t exist, so i can make another one.
// for ex
int i = 5;

with g++ it''s ok it follows that rule

but with visual c++
the variable i still exists after the }
and i don''t want that to happen because the code won''t be portable under linux.


after the 1st for
i can''t make int i=5, because it says that the variable i already exists. Maybe it''s a feature of visual c++, but i would like the compiler to act as the norm says, and i don''t know how...

Share this post


Link to post
Share on other sites
owl    376
quote:
Original post by Anonymous Poster
I dont understand what the problem with that is, why not just say.
or


for(int i=0;i<10;i++)
{
...

}
for(int j=0;j<10;j++)
{
...

}

???


I think this jumps as double declaration in VC++

Share this post


Link to post
Share on other sites
airjy    122
yes it''s double declaration ant that''s the pb...
maybe i''ll have to swith to visual studio .net if that''s fixed.
maybe for you it''s not a bug, but for me it''s a huge one

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by owl
quote:
Original post by Anonymous Poster
I dont understand what the problem with that is, why not just say.
or


for(int i=0;i<10;i++)
{
...

}
for(int j=0;j<10;j++)
{
...

}

???


I think this jumps as double declaration in VC++



I dont think that is a double decleration, i changed the variable name to ''j'' the second time.

Share this post


Link to post
Share on other sites
owl    376
quote:
Original post by Anonymous Poster
quote:
Original post by owl
quote:
Original post by Anonymous Poster
I dont understand what the problem with that is, why not just say.
or


for(int i=0;i<10;i++)
{
...

}
for(int j=0;j<10;j++)
{
...

}

???


I think this jumps as double declaration in VC++



I dont think that is a double decleration, i changed the variable name to ''j'' the second time.



Oh, look at that!

Share this post


Link to post
Share on other sites
MauMan    344
This was one of those controversial "breaking changes" between pre-ansi and ansi C++ that microsoft chose to support the old behavior for VC6. I remember seeing an article somewhere on microsoft.com sugesting that you do the following trick/hack/kludge to get the right behavior on VC6:

#define for if ( true ) for

Share this post


Link to post
Share on other sites
airjy    122
sorry i didn''t see that you had changed i to j.
i can change the name of the variable each time i have to make a for or to write i=0 juste before starting the for, but when you''re used to write the same thing : for(int i=...) without even thinking what i''m writing it''s hard to change

Share this post


Link to post
Share on other sites
Sneftel    1788
It''s a bug in VC++ 6.0. It''s fixed in .NET, with an option to undo the fix (for compatibility with 6.0 code).


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
krez    443
i have heard there is an option somewhere to change this so it works the proper ANSI way (i don''t have any details though, since i don''t use that compiler/IDE).

Share this post


Link to post
Share on other sites
Beer Hunter    712
quote:
Original post by MauMan
#define for if ( true ) for
That's not it; that'll behave oddly if followed be by else.

#define for if(0); else for

[edited by - Beer Hunter on September 3, 2003 6:35:11 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I still don''t see why its that much of a problem, although I have been used to VC++ for a long time, so maybe its just me.

Share this post


Link to post
Share on other sites
MauMan    344
krez : The option is to "disable microsoft extensions" but it breaks the version of STL it ships with and other things so the cure is worse than the problem.

Share this post


Link to post
Share on other sites
krez    443
quote:
Original post by MauMan
krez : The option is to "disable microsoft extensions" but it breaks the version of STL it ships with and other things so the cure is worse than the problem.

that''s what you get

Share this post


Link to post
Share on other sites