"ofstream" as function argument

Shibli picture Shibli · Mar 11, 2012 · Viewed 53.7k times · Source

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

Answer

Boris Strandjev picture Boris Strandjev · Mar 11, 2012

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.