【发布时间】:2020-05-14 12:21:36
【问题描述】:
我在声明继承类的构造函数时遇到问题。
class Report{
public:
string fileName;
std::ofstream outputFile;
Report(string fileName, ofstream outputFile) {
fileName = fileName;
outputFile = outputFile; //<-- error here
}
void returnFile(string, ofstream);
void Report::returnFile(string name, ofstream file){
file.open(name);
}
};
class financialReport: public Report{
public:
void electorateHappenings();
void electorialImpact();
double finances();
void writetoFile();
financialReport(string fileName, ofstream outputFile)
:Report(fileName, outputFile) { } //<-- error here
};
错误发生在第三行最后一行:Report(fileName, outputFile)。
此行产生错误:
function "std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const
std::basic_ofstream<_CharT, _Traits> &) [with _CharT=char,
_Traits=std::char_traits<char>]" (declared at line 848 of
"C:\MinGW\lib\gcc\mingw32\9.2.0\include\c++\fstream") cannot be referenced
-- it is a deleted function
不能创建包含ofstream的构造函数吗?
错误也发生在第 9 行 outputFile = outputFile。
谢谢。
【问题讨论】:
-
参数名称不能与成员变量相同。
-
@gavinb 仍然不起作用
-
@gavinb - 可以。尽管存在风格上的争论。
-
这是XY problem 的经典例子。你想达到什么目的?您试图以不正确的方式执行此操作并要求修复无效的解决方案。可悲的是,有人已经做出了错误的编译(和工作)方法。
标签: c++ inheritance constructor ofstream