为什么在传递 std::ofstream 作为参数时要"use of deleted"函数?
Why do I make "use of deleted" function when passing a std::ofstream as parameter?
我有一个成员是std::ofstream fBinaryFile
和a
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
语义确实支持复制。
根据您的体系结构,您可以存储或移动指针/引用。
如果你想复制pBinaryFile的内容到fBinaryFile,你需要将pBinaryFile声明为ifstream(输入文件流),而不是ofstream(输出文件流)它应该看起来像这样:
std::ifstream pBinaryFile;
std::ofstream fBinaryFile;
std::stringstream sstream;
std::string line
pBinaryFile.open(pBinaryFileName.c_str());
fBinaryFile.open(fBinaryFileName.c_str());
if (pBinaryFile.isopen()) {
while (pBinaryFile.good()) {
getline(pBinaryFile, line);
fBinaryFile << sstream(line) << endl;
}
}
pBinaryFile.close();
fBinaryFile.close();
注意pBinaryFileName和fBinaryFileName是指你的文件路径。
这段代码可能有错误,但我认为解决方案看起来像这样。
我建议进一步阅读:
http://www.cplusplus.com/doc/tutorial/files/相关文章:
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- C++ "error: invalid use of void expression"
- 我看到"use of undeclared identifier"错误,有人可以告诉我如何解决它吗?
- 收到错误"invalid use of non-static data member 'stu::n' "
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- C++ "Invalid use of 'this' in non-member function" ,
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- C++14 遇到奇怪的"use of deleted function"错误
- 出现这种错误的原因是什么"invalid use of non-static data member "
- 模板类和'invalid use of incomplete type'错误
- "invalid use of incomplete type" .解决循环依赖关系
- 使用互斥锁和条件变量作为成员时如何修复"use of deleted function"?
- 构造函数导致"use of deleted function"错误
- 禁止显示"use of the 'X' attribute is a C++17 extension"警告
- 为什么包含标头会导致"use of auto before deduction of auto"错误?
- C++:避免"use of deleted function"错误
- VSCode 错误: "incorrect use of va_start"
- 为什么我会得到"Invalid use of :: (scope resolution operator)?"
- 如何修复库中的"invalid use of non-static member function" 导入另一个库
- 在C++中传递对象会产生'invalid use of non-static member'错误