From: Weiqi Gao Newsgroups: comp.os.msdos.djgpp Subject: Re: "for" messages Date: Mon, 22 Nov 1999 23:49:22 -0600 Organization: CRL Network Services Lines: 84 Message-ID: <383A2AE2.9F313FDA@a.crl.com> References: <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> <055AF6C058B39CA5 DOT AE73F39F516148F5 DOT 649AD2B8ADF2213B AT lp DOT airnews DOT net> NNTP-Posting-Host: a116024.stl1.as.crl.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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: > > 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