(i/o streams) making a function to output to a file

Hello, I am trying to make a function called

void print()

but i don't know exactly how to write the function parameters

here is how my main would look like as an example.
1
2
3
4
5
6
7
8
9
10
//main.cpp

vector a; //Assume it has already been filled with elements

ofstream output(argv[2]) //the name in argv[2] is what i want to write to

if(!output) {
cerr << "error"; return 1; }

a.print( output ); // assume the function, print, would print the vector in a file correctly. 


now, if i call
a.print( output );
is that correct?

how would i write the function parameters in the interface and implementation files?

I've tried doing,

void print( ofstream& outFile );
but it gives me the error saying
error: ‘ofstream’ has not been declared
and also it gives me this error
1
2
3
../main.cpp: In function ‘int main(int, char**)’:
../main.cpp:86: error: invalid conversion from ‘void*’ to ‘int’
../main.cpp:86: error:   initializing argument 1 of ‘void Class::print(int)’

for the function call in the main file


How do i fix this? thanks!
Last edited on
Did you #include the header file for ofstream?
yes i did.
found out that in my implementation file, the name was not correct. some errors have changed but most are still the same.

this is what i get now.
1
2
3
4
../class.h:14: error: ‘ofstream’ has not been declared
../main.cpp: In function ‘int main(int, char**)’:
../main.cpp:86: error: no matching function for call to ‘Class::print(std::ofstream&)’
../class.h:14: note: candidates are: void class::print(int&)
Last edited on
nevermind, i forgot to add the std:: in front of ofstream. thanks!
Topic archived. No new replies allowed.