Scoping in for loop

Dimesh's Avatar, Join Date: Jul 2010
Go4Expert Member
Al salamo Alykom

And hi..
Thank you for reading my topic
------------------------------

After building this code:

Code:
#include<iostream>

	using namespace std;

	int main()
	{
		for(int i=0; i<5 ; i++)
		{
			cout << "i:" << i << endl;
		}
		i = 7;
		return 0;
	}
I found this problem :
i read that the problem means my compiler does not support this aspect of the ANSI standard...
what is meant by that and how can i make my compiler support or not ?!


**** Build of configuration Debug for project Linux_Xp_Learning ****

make all
Building file: ../scoped_for_loop.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"scoped_for_loop.d" -MT"scoped_for_loop.d" -o"scoped_for_loop.o" "../scoped_for_loop.cpp"
../scoped_for_loop.cpp: In function ‘int main()’:
../scoped_for_loop.cpp:19: error: name lookup of ‘i’ changed for ISO ‘for’ scoping
../scoped_for_loop.cpp:19: note: (if you use ‘-fpermissive’ G++ will accept your code)
make: *** [scoped_for_loop.o] Error 1
Ancient Dragon's Avatar, Join Date: Jul 2010
Go4Expert Member
That is not ansi standard. If you originally wrote that using VC++ 6.0 compiler it worked because that was a non-standard c++ compiler. Microsoft has since changed their ways and made vc++ 2010 much more ansi standard compliant.

>>how can i make my compiler support or not
Your compiler IS supporting ansi standard -- it was the code you posted that was not ansi standard compliant, not the compiler.
Ancient Dragon's Avatar, Join Date: Jul 2010
Go4Expert Member
One way to make your code correct is to declare variable i outside and above the loop, very much linke C programs do
Code:
int main()
{
   int i;
   for(i = 0; i < 5; i++)
   {
      // blabla
   }
   i = 0;
}

Last edited by Ancient Dragon; 25Jul2010 at 23:31..
shabbir like this