Another strange compile error with visual studio c++ 6.0

G

Gunnar G

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: "initialization of y is skipped by
case label" was reported.

int main(){
int x[10];
int a=4;
switch (a){
case 1: for (int y=0;y<10;y++) x[y]=y; break;
case 2: a=5;
}
}

Thanks,
Gunnar.
 
J

Jens Marder

Simple: The C++ implementation in VS6 C++
doesn't allow var-declarations in for-parantheses!
Mind:It's an old compiler, from the mid-late 90s !
Wasn't FULLY compliant with C++ standards.
 
A

Alf P. Steinbach

* Gunnar G:
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: "initialization of y is skipped by
case label" was reported.

int main(){
int x[10];
int a=4;
switch (a){
case 1: for (int y=0;y<10;y++) x[y]=y; break;
case 2: a=5;
}
}

MSVC 6.0 followed the pre-standard rules where the scope of y didn't end with
the loop, but extended to the end of the block the loop is placed in (and
that's still the default in MSVC 7.1, which is the one I use). With this
non-standard treatment y can be access in case 2, without having been
initialized. The problem is not with the declaration but with the compiler
using old rules where the scope of y extends a bit too far.

Try a more modern -- or less antiquated ;-) -- compiler.

For Microsoft's, add the necessary compiler options to get near standard
behavior (turning on exception handling, RTTI, for-loop conformance etc.).
 
M

Mike Wahler

Jens Marder said:
Simple: The C++ implementation in VS6 C++
doesn't allow var-declarations in for-parantheses!

Not true. (However it does get scoping rules wrong).
Mind:It's an old compiler, from the mid-late 90s !
Wasn't FULLY compliant with C++ standards.

That's true, but nothing to do with OP's problem.

Gunnar: Did you look up the error in the help file?
It explains what you did wrong.

-Mike
int main(){
int x[10];
int a=4;
switch (a){
case 1: for (int y=0;y<10;y++) x[y]=y; break;
case 2: a=5;
}
}

Thanks,
Gunnar.
 
G

Gunnar G

Gunnar: Did you look up the error in the help file?
It explains what you did wrong.
I regret to say "no", since the problem was only presented to me by someone
that I thought would have looked there.
But I'll have a look there as soon as I can.
Thanks for answering..
 
J

Jim Langston

Gunnar G said:
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: "initialization of y is skipped
by
case label" was reported.

int main(){
int x[10];
int a=4;
switch (a){
case 1: for (int y=0;y<10;y++) x[y]=y; break;
case 2: a=5;
}
}

Thanks,
Gunnar.

It's just M$ VC being stupid. The same thing would happen if it wasn't
initialized in a for statement. The compiler is presuming (wrongly in this
case) that you want to use y outside the case statement. Simple solution,
put the code in brackets and it'll be happy.

case 1: { for (int y=0;y<10;y++) x[y]=y; break; };
case 2: { a=5; } // Brackets not needed here.

This just lets the compiler know that teh scope of y is within the case
statement only.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top