Find the answer to your Linux question:
Results 1 to 4 of 4
dear linux user/programer with c++: my name is eric, when I use most current g++ 4.3.3 on ubuntu to test 2 simple c++ programs, one have compiler error, -------------------- // ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5

    C++ Compilation Error


    dear linux user/programer with c++: my name is eric, when I use most current g++ 4.3.3 on ubuntu to test 2 simple c++ programs, one have compiler error,
    --------------------
    // formore.cpp -- more looping with for

    #include <iostream>

    using namespace std;

    const int ArSize = 16; // example of external declaration

    int main()

    {

    double factorials[ArSize];

    factorials[1] = factorials[0] = 1.0;

    // int i;

    for (int i = 2; i < ArSize; i++)

    factorials[i] = i * factorials[i-1];

    for (i = 0; i < ArSize; i++)

    cout << i << "! = " << factorials[i] << endl;

    return 0;

    }
    --------------------------------------------------------
    eric@eric-laptop:~/Documents/c++primerplus/download/ch05$ g++ formore.cpp
    formore.cpp: In function ‘int main()’:
    formore.cpp:12: error: name lookup of ‘i’ changed for ISO ‘for’ scoping
    formore.cpp:12: note: (if you use ‘-fpermissive’ G++ will accept your code)
    --------------------------
    or even more simpler
    --------
    #include <iostream>
    using namespace std;

    int main () {



    for (int i=0; i<5; i++)
    cout << "C++ know loops.\n";

    cout << i << endl;


    return(0);
    }
    --------------------
    eric@eric-laptop:~$ g++ try.c++
    try.c++: In function ‘int main()’:
    try.c++:11: error: name lookup of ‘i’ changed for ISO ‘for’ scoping
    try.c++:11: note: (if you use ‘-fpermissive’ G++ will accept your code)
    ------------------

    --------------------
    the other is OK
    ---------------
    // bigstep.cpp -- count as directed

    #include <iostream>

    int main()

    {

    using namespace std;

    cout << "Enter an integer: ";

    int by;

    cin >> by;

    cout << "Counting by " << by << "s:\n";

    for (int i = 0; i < 100; i = i + by)

    cout << i << endl;

    return 0;

    }
    -----------------------
    eric@eric-laptop:~/Documents/c++primerplus/download/ch05$ g++ bigstep.cpp
    eric@eric-laptop:~/Documents/c++primerplus/download/ch05$ ./a.out
    ------------
    but you can see both programs, their for loop are very similar(or the same)
    for (int i=0
    but why first , and second programs can not compile
    but
    the third one is OK
    ?
    please advice, thanks in advance your effort and time, fsshl@luxmail.com
    Last edited by Cabhan; 10-29-2010 at 02:31 AM.

  2. #2
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    Becasue in the first two, you use the second "i" without typing it and it is out of the scope of the previous for loop.

    If you bracket your for loops, you can clearly see it is out of scope.

    Code:
    for(int i=0;i<9;i++)
    {
         //in scope
         cout << i;
    }
    
    //not in scope
    cout << i;

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,517
    Please don't expect help with homework assignments in these forums. It is not allowed per forum usage rules. I apologize if this is not a school homework thing, but just you trying to learn C++. Please clarify your situation.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,517
    Your code:
    Code:
    for (int i = 2; i < ArSize; i++)
    
    factorials[i] = i * factorials[i-1];
    
    for (i = 0; i < ArSize; i++)
    
    cout << i << "! = " << factorials[i] << endl;
    Rewritten with explicit scoping and indentation for readability:
    Code:
    for (int i = 2; i < ArSize; i++)
    {
        factorials[i] = i * factorials[i-1];
    }
    for (i = 0; i < ArSize; i++)
    {
        cout << i << "! = " << factorials[i] << endl;
    }
    Note that in the second loop, the variable 'i' does not have a type specified. In standard C++, loop variables (variables declared inside the ()) are local to the loop and are not visible outside of it. So, in the case of the second loop, 'i' has not been declared, so the compiler gave you the error you saw.
    Last edited by Rubberman; 10-29-2010 at 07:19 PM.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •