ofil ???

This is a discussion on ofil ??? within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm slugging it out with Stan Lippman (Essential C++) and Stan's winning. I've included fstream, but when we hit ...

  1. #1
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79

    ofil ???

    Hey, I'm slugging it out with Stan Lippman (Essential C++) and Stan's winning.

    I've included fstream, but when we hit
    Code:
    ofil << "about to call swap!";
    I get an expected - primary expression error.

    Meanwhile I can't find ofil in any of my references. What gives?

    - JM
    Last edited by -JM; 09-02-2005 at 09:43 PM. Reason: typo

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Isn't ofil the identifier given to an ostream in that example?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    -JM - post your whole code, and tell us what compiler you're using (that way, if we have the documentation, we can look up the error and get a better description of exactly what the problem is).

  4. #4
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    okey dokey -

    Code:
    #include<iostream>
    #include<vector>
    #include<fstream>
    using namespace std;
    
    
    void display( vector<int> vec)
    {
         for ( int ix = 0; ix < vec.size(); ++ix)
             cout<<vec[ix]<<' ';
         cout<<endl;
    }
    
    void swap( int &val1, int &val2)
    {
         std::ofstream ofil<<"swap( "<<val1
             <<", "<<val2<<" )\n";
             
         int temp = val1;
         val1 = val2;
         val2 = temp;
         
         ofil<<"after swap(): val1 "<<val1
             <<" val2: "<<val2<<"\n";
    }
    
    ofstream ofil("E:\\text_out1");
    void bubble_sort(vector<int> &vec)
    {
         for(int ix = 0; ix<vec.size(); ++ix)
             for(int jx = ix+1; jx<vec.size(); ++jx)
                if(vec[ix]>vec[jx]){
                //debugging output
                std::ofstream ofil<<"about to call swap!"
                    <<" ix: "<<ix<<" jx: "<<jx<<'\t'
                    <<" swapping: "<<vec[ix]
                    <<" with "<<vec[jx]<<endl;
                    //ok actual swap code...
                   swap(vec[ix], vec[jx]);
                }
    }
    
    int main()
    {
        int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2};
        vector<int> vec(ia, ia+8);
        
        cout<<"vector before sort: ";
        display(vec);
        
        bubble_sort(vec);
        
        cout<<"vector after sort: ";
        display(vec);
        
        cin.get();
        return 0;
    }
    I'm using Dev C++, and I get "expected primary-expression before 'ofil' " and "expected ';' before ofil"

    I still don't get it!

    - JM

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    It may be that in the function swap() you are using ofil but you declare it afterwards.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    I was really hoping that was it, but no such luck. I still get those errors on all three lines with 'ofil'.

    Anybody??

    I'm missing something to do with ofstream....

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    ofstream ofil("E:\\text_out1");
    put this line just after using namespace std;

    in swap
    Code:
         std::ofstream ofil<<"swap( "<<val1
    should be
    Code:
         ofil<<"swap( "<<val1
    and in bubble_sort
    Code:
                std::ofstream ofil<<"about to call swap!"
    should be
    Code:
                ofil<<"about to call swap!"
    Kurt

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    swap() function is coded incorrecvtly. ofil must be an open stream, which it isn't. Sice swap() will be called many many times, suggest you pass it a reference to an open stream.

    Quote Originally Posted by -JM
    okey dokey -

    Code:
    #include<iostream>
    #include<vector>
    #include<fstream>
    using namespace std;
    
    
    void display( vector<int> vec)
    {
         for ( int ix = 0; ix < vec.size(); ++ix)
             cout<<vec[ix]<<' ';
         cout<<endl;
    }
    
    void swap( int &val1, int &val2, ofstream& ofil)
    {
         ofil<<"swap( "<<val1
             <<", "<<val2<<" )\n";
             
         int temp = val1;
         val1 = val2;
         val2 = temp;
         
         ofil<<"after swap(): val1 "<<val1
             <<" val2: "<<val2<<"\n";
    }
    
    ofstream ofil("E:\\text_out1");
    void bubble_sort(vector<int> &vec, ofstream& ofil)
    {
         for(int ix = 0; ix<vec.size(); ++ix)
             for(int jx = ix+1; jx<vec.size(); ++jx)
                if(vec[ix]>vec[jx]){
                //debugging output
                ofil<<"about to call swap!"
                    <<" ix: "<<ix<<" jx: "<<jx<<'\t'
                    <<" swapping: "<<vec[ix]
                    <<" with "<<vec[jx]<<endl;
                    //ok actual swap code...
                   swap(vec[ix], vec[jx], ofil);
                }
    }
    
    int main()
    {
        int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2};
        vector<int> vec(ia, ia+8);
        
        cout<<"vector before sort: ";
        display(vec);
        [color=red]ofstream ofil("myfile.txt");    
        bubble_sort(vecofil);
        
        cout<<"vector after sort: ";
        display(vec);
        
        cin.get();
        return 0;
    }

    - JM

  9. #9
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Thanks guys, now it works -

    ZuK, I did just as you said and it works perfectly.

    Ancient Dragon, the swap function is straight out of the Lippman book so I didn't change it - maybe it's not coded correctly because it's early in the book?

    Anyway, good to go now, thanks a lot!

    - JM

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You should do what Ancient Dragon says because global variables are baaaad.
    Kurt

  11. #11
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Code:
        bubble_sort(vec[color=red]ofil);
    'color' undeclared (first use this function)
    'red' undeclared (first use this function)

    now she no work again

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    bubble_sort(vec,ofil);

  13. #13
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    OK works again, thanks.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21