Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Scoping in for loop (http://www.go4expert.com/forums/scoping-loop-t22809/)

Dimesh 25Jul2010 20:35

Scoping in for loop
Al salamo Alykom

And hi..
Thank you for reading my topic

After building this code:



        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 25Jul2010 22:49

Re: Scoping in for loop
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 25Jul2010 22:51

Re: Scoping in for loop
One way to make your code correct is to declare variable i outside and above the loop, very much linke C programs do

int main()
  int i;
  for(i = 0; i < 5; i++)
      // blabla
  i = 0;

All times are GMT +5.5. The time now is 04:05.