我在声明继承类的构造函数时遇到问题。
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
};
错误发生在最后3行: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
。
谢谢。
您不能通过复制传递它,也不能复制一个,但是可以通过引用传递它并在类的初始化器列表中对其进行初始化:
class Report {
public:
string fileName;
std::ofstream &outputFile; //reference here
// reference parameter, and initializer list
Report(string fileName, ofstream &outputFile) : outputFile(outputFile) {
fileName = fileName;
}
//...
};
在financialReport
:
financialReport(string fileName, ofstream& outputFile) : Report(fileName, outputFile) {}
^
请注意,这通常是对问题的解决方案,但是在更深入的分析中,尽管您没有详细说明要实现的目标,但我不会说它是这是错误的方法,但是您可能会以更好的方式构造程序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句