“ofstream” as function argument

北慕城南 提交于 2019-11-28 06:16:14

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.

Joel Falcou

You have to pass a reference to the ostream object as it has no copy constructor:

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

If you are using a C++11 conformant compiler and standard library, it should be ok to use

void foo(std::ofstream dumFile) {}

as long as it is called with an rvalue. (Such calls will look like foo(std::ofstream("dummy.txt")), or foo(std::move(someFileStream))).

Otherwise, change the parameter to be passed by reference, and avoid the need to copy/move the argument:

void foo(std::ofstream& dumFile) {}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!