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
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
Of course there is. Just use reference. Like that:
Otherwise the copy constructor will be invoked, but there is no such defined for the class
ofstream
.If you are using a C++11 conformant compiler and standard library, it should be ok to use
as long as it is called with an rvalue. (Such calls will look like
foo(std::ofstream("dummy.txt"))
, orfoo(std::move(someFileStream))
).Otherwise, change the parameter to be passed by reference, and avoid the need to copy/move the argument:
You have to pass a reference to the
ostream
object as it has no copy constructor: