Shi*_*bli 33 c++ function ofstream
有没有办法将输出流作为参数传递
void foo (std::ofstream dumFile) {}
我试过了,但它给了
error : class "std::basic_ofstream<char, std::char_traits<char>>" has no suitable copy constructor
Bor*_*jev 43
当然有.只需使用参考.像那样:
void foo (std::ofstream& dumFile) {}
Run Code Online (Sandbox Code Playgroud)
否则将调用复制构造函数,但是没有为该类定义ofstream.
您必须传递ostream对象的引用,因为它没有复制构造函数:
void foo (std::ostream& dumFile) {}
Run Code Online (Sandbox Code Playgroud)
如果您使用符合C++ 11的编译器和标准库,则可以使用它
void foo(std::ofstream dumFile) {}
Run Code Online (Sandbox Code Playgroud)
只要用rvalue调用它.(这样的电话看起来像foo(std::ofstream("dummy.txt")),或foo(std::move(someFileStream))).
否则,更改要通过引用传递的参数,并避免复制/移动参数:
void foo(std::ofstream& dumFile) {}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45112 次 |
| 最近记录: |