Scoping in for loop

Discussion in 'C' started by Dimesh, Jul 25, 2010.

  1. Dimesh

    Dimesh New Member

    Joined:
    Jul 12, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. Ancient Dragon

    Ancient Dragon New Member

    Joined:
    Jul 23, 2010
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    part time cashier at WalMart
    Location:
    near St Louis, IL, USA
    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.
     
  3. Ancient Dragon

    Ancient Dragon New Member

    Joined:
    Jul 23, 2010
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    part time cashier at WalMart
    Location:
    near St Louis, IL, USA
    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: Jul 25, 2010

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice