Mail Archives: djgpp/1999/11/23/02:06:57
Rodeo Red wrote:
>
> Weiqi Gao wrote:
>
> > The code in your file used to be legal C++ code, but the final ANSI C++
> > standard made it illegal.
> >
>
> By "illegal" you mean the program won't compile ? Then these are error messages and
> not just warnings ? (How does one ultimately tell ?)
Warning messages are flagged as so, as in:
foo.cc:3: warning: variable 'i' defined but never used
> > The thing to note here is that the scope of a variable declared in the
> > head of a loop is the rest of the head and inside of the braces:
> >
> > for (/* declare variable here */; /* use it here */ ; /* use it here
> > */) {
> > /* use it here */
> > }
> > /* can't use it here */
>
> according to what your saying I should add braces to lines 20 and 26- {}
> for example change:
No the braces here are not important.
> line 19 for (i = 0; i < 5; i++)
> line 20 cout << "num[" << i <<"] == " << num[i] << '\n';
>
> to [...]
>
> I thought that shouldnt matter because the i is also declared in the second loop.
Here's the key to your troubles! You did not declare the 'i' in your
second for() loop:
for (i = 0; i < 5; i++) {
// stuff
}
"i = 0" is an assignment. For it to be legal, 'i' must have been
declared earlier in the scope.
You can make it a declaration by changing "i = 0" to "int i = 0".
> > The error messages you received from the compiler is an attempt by the
> > gcc authors to help users who know the old rule (which says you CAN use
> > the variable). Had they been less considerate, you would have received
> > this error message:
> >
> > testfile.cpp:25: 'i' undeclared (first use in this function)
>
> OK so they ARE error messages. That makes sense but I still don't understand what
> the fix is.
Change your
for (int i = 0; i < 5; i++) {
// stuff
}
for (i = 0; i < 5; i++) {
// stuff
}
into
for (int i = 0; i < 5; i++) {
// stuff
}
for (int i = 0; i < 5; i++) {
// stuff
}
And mentally realize that the 'i' in the second for() loop has
absolutely nothing to do with the one in the first for() loop.
> Thanks for that clarification anyway. Onward through the fog!
--
Weiqi Gao
weiqigao AT a DOT crl DOT com
- Raw text -