This question already has answers here:
Why copying stringstream is not allowed?

(3个答案)



C++ copy a stream object

(5个答案)


5年前关闭。




我有一个成员,即std::ofstream fBinaryFile和一个
void setFile( std::ofstream& pBinaryFile )
{
    fBinaryFile = pBinaryFile;
}

输出:
 Data.h:86:16: error: use of deleted function ‘std::basic_ofstream<char>& std::basic_ofstream<char>::operator=(const
 std::basic_ofstream<char>&)’
     fBinaryFile = pBinaryFile;
                 ^

我了解不允许使用std::ofstream复制,也许我丢失了一些东西。可以将pBinaryFile的内容保存在fBinaryfile中吗?

最佳答案

因为相关的运算符被声明为

ofstream& operator= (const ofstream&) = delete;

这意味着它被明确禁止,因此ofstream语义确实支持复制。

根据您的体系结构,您可以存储指针/引用或移动它。

关于c++ - 当传递std::ofstream作为参数时,为什么要使 “use of deleted”函数? [复制],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31674353/

10-11 22:25
查看更多