Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ofstream" as function argument

Is there a way to pass output stream as argument like

void foo (std::ofstream dumFile) {}

I tried that but it gave

error : class "std::basic_ofstream<char, std::char_traits<char>>" has no suitable copy constructor

like image 951
Shibli Avatar asked Mar 11 '12 20:03

Shibli


People also ask

Is ofstream a function?

Both istream and ostream provide member functions for repositioning the file-position pointer. These member functions are seekg ("seek get") for istream and seekp ("seek put") for ostream.

Can you pass an ifstream as a parameter C++?

If you get an ifstream as parameter, it should be open from the start because you opened it outside your function. Passing a stream and then opening it inside your function does not make sense.


1 Answers

Of course there is. Just use reference. Like that:

void foo (std::ofstream& dumFile) {} 

Otherwise the copy constructor will be invoked, but there is no such defined for the class ofstream.

like image 178
Boris Strandjev Avatar answered Oct 03 '22 13:10

Boris Strandjev