-
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
-
Isn't ofil the identifier given to an ostream in that example?
-
-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).
-
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
-
It may be that in the function swap() you are using ofil but you declare it afterwards.
-
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....
-
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
-
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
-
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
-
You should do what Ancient Dragon says because global variables are baaaad.
Kurt
-
Code:
bubble_sort(vec[color=red]ofil);
'color' undeclared (first use this function)
'red' undeclared (first use this function)
now she no work again
-
Code:
bubble_sort(vec,ofil);
-