the i problem

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
muntaseri
New poster
Posts: 1
Joined: Sat Sep 27, 2003 2:33 pm

the i problem

Post by muntaseri »

Here are the compiler error messages:

01926502_24.c: In function `int main(...)':
01926502_24.c:26: name lookup of `i' changed for new ANSI `for' scoping
01926502_24.c:14: using obsolete binding at `i'


what is the problem
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

Assuming you have code sort of like the following:

[cpp]
int main ()
{
int i;

for (int i = 0; i < 10; i++) // this "i" and the "i" above
// clash with one another.. remove one or the other
printf ("Hello world!\n");
}
[/cpp]
Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise »

I had the same problem as you, but with the following code :
[cpp]
// no i global scope variable
int main(){
// no i local scope variable
for (int i=0; i<150; i++) {
// i is only preesnt in the foor loop
// work with i;
}
printf("%d", i); // <---- HERE ! i is used but out of scope
return 0;
}
[/cpp]
The compiler sees that you're using the i defined in the for, thus trying to implicitely change its scope, wich is forbidden.
Such cases arise when you're using the for-loop to do a sequential search, and you then use the index of the found element.
To solve it, simply move int i into main()'s scope, not for(s scope.[/cpp]
Post Reply

Return to “C++”