delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/11/23/06:15:09

From: Weiqi Gao <weiqigao AT a DOT crl DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: "for" messages
Date: Mon, 22 Nov 1999 20:37:56 -0600
Organization: CRL Network Services
Lines: 44
Message-ID: <3839FE04.9F9FEDF8@a.crl.com>
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>
NNTP-Posting-Host: a116002.stl1.as.crl.com
Mime-Version: 1.0
X-Mailer: Mozilla 4.51 [en] (X11; I; Linux 2.2.5-15 i586)
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

Rodeo Red wrote:
> 
> Peter Danielsson wrote:
> 
> > They are only warnings and should compile anyway.
> 
> The plot thickens.
> Here is the exact command I used to try to compile
> 
> C:\djgpp\Source>gpp testfile.cpp -o testfile.exe
> 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'
> 
> There's no testfile.exe. As far as I know, that means it didn't compile. What
> would make this happen ?
> Is the problem with the code ? or are my djfpp files corrupted ?

The code in your file used to be legal C++ code, but the final ANSI C++
standard made it illegal.

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 */

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.

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)

-- 
Weiqi Gao
weiqigao AT a DOT crl DOT com

- Raw text -


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