delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/11/23/00:24:58

From: Rodeo Red <rodeored AT netstep DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: "for" messages
Date: Mon, 22 Nov 1999 22:56:30 -0500
Organization: Church of Evangelical Environmental Extremism
Lines: 77
Message-ID: <055AF6C058B39CA5.AE73F39F516148F5.649AD2B8ADF2213B@lp.airnews.net>
X-Orig-Message-ID: <383A106C DOT C957E74D AT netstep DOT net>
References: <F0458523BAB82511 DOT F75DE6F80A0A31D6 DOT 8D4647641CEFD2ED AT lp DOT airnews DOT net> <3839D8B8 DOT F0E10FB AT efd DOT lth DOT se> <31D677D3D5976EA1 DOT 2282C56AE95402D6 DOT 16BA81A6C65620CB AT lp DOT airnews DOT net> <3839FE04 DOT 9F9FEDF8 AT a DOT crl DOT com>
Abuse-Reports-To: support at netstep.net to report improper postings
NNTP-Proxy-Relay: library1.airnews.net
NNTP-Posting-Time: Mon Nov 22 21:54:57 1999
NNTP-Posting-Host: !b?L"-@[06`4cRN (Encoded at Airnews!)
Mime-Version: 1.0
X-Mailer: Mozilla 4.7 [en] (Win98; I)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com


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 ?)

>
> 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:
 line 19     for (i = 0; i < 5; i++)
line  20    cout << "num[" << i <<"] == " << num[i] << '\n';

to

line  19  for (i = 0; i < 5; i++)
 line  20    {
          cout << "num[" << i <<"] == " << num[i] << '\n';
                 }

{
    /* Then I'm using it here */
  }

So I tried that and I got the same messages:
testfile.cpp: In function `int main(...)':
testfile.cpp:25: name lookup of `i' changed for new ANSI `for' scoping
testfile.cpp:19:   using obsolete binding at `i'

Line 25 is:    for (i = 0; i < 5; i++)
Line 19 is:     for (int i = 0; i < 5; i++)

which is not suprizing because I added braces to lines 26 and 20, but the errors
are in lines 25 and 19.

>
>
> Your second for() loop is trying to use the  'i'  declared in the head
> of the first for() loop, which by the time you reached the second for()
> loop has gone out of scope already.
>

I thought that shouldnt matter because the i is also declared in the second loop.

>
> 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.
Thanks for that clarification anyway. Onward through the fog !

Red

--


"Space travel is utter bilge!"  -Sir Richard Van Der Riet Wolley


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019