I'm having issues declaring a constructor of an inherited class.
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
};
the error occurs on the 3rd last line :Report(fileName, outputFile)
.
This line produces the error:
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
Is it not possible to create a constructor including ofstream
?
The error also occurs on line 9 with outputFile = outputFile
.
Thank you.
You can't pass it by copy, you can't copy one, but you can pass it by reference and initialize it in the initializer list of the class:
class Report {
public:
string fileName;
std::ofstream &outputFile; //reference here
// reference parameter, and initializer list
Report(string fileName, ofstream &outputFile) : outputFile(outputFile) {
fileName = fileName;
}
//...
};
Do the same in financialReport
:
financialReport(string fileName, ofstream& outputFile) : Report(fileName, outputFile) {}
^
Note that this is a solution to the problem posed in the question, as normal, but in a more deep analysis, though you don't go in detail about what you want to achieve, I wouldn't go so far as to say it's a wrong approach, but odds are you can structure your program in a better way.