for and visual c++ 6.0

Started by
16 comments, last by airjy 20 years, 7 months ago
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.
Advertisement
It doesn''t conform to the C++ standard in this aspect. Visual Studio .NET fixed this (I think, though I''m pretty sure).
______________________________"Man is born free, and everywhere he is in chains" - J.J. Rousseau
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++)
{
...

}

???
I think it was

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

for(i = 11; i < 20; i++) // notice i left out "int"
{...}
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...

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++
[size="2"]I like the Walrus best.
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
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.
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!
[size="2"]I like the Walrus best.
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

---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }

This topic is closed to new replies.

Advertisement