我可以使用在 class 构造函数中初始化的 ofstream 类型的成员变量吗?
Can I use a member variable of type ofstream initialized in the class constructor?
我在声明继承 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
};
错误发生在倒数第 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.2.0\include\c++\fstream") cannot be referenced
-- it is a deleted function
是否不能创建包含ofstream
的构造函数?
错误也发生在第 9 行 outputFile = outputFile
。
谢谢。
不能拷贝传,不能拷贝一个,但是可以传引用,在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;
}
//...
};
在financialReport
中做同样的事情:
financialReport(string fileName, ofstream& outputFile) : Report(fileName, outputFile) {}
^
请注意,这是对问题中提出的问题的解决方案,正常情况下,但在更深入的分析中,虽然您没有详细说明您要实现的目标,但我不会这样做虽然可以说这是一种错误的方法,但您很有可能会以更好的方式构建您的程序。
是的,可以,但是错误提示您不能复制 std::ofstream
的对象。
根据你想做什么,有两种处理方式。
将 std::ofstream
的所有权传递给您新创建的对象:
Report(string fileName, ofstream outputFile) :
fileName{std::move(outputFile)},
outputFile{std::move(outputFile)}
{
}
//creation of object:
std::ofstream ofs {"filename.txt"};
Report report {"filename.txt", std::move(ofs)};
//ofs is empty here, it's whole content has been transferred to report object
传递对现有 std::ofstream
对象的引用:
class Report{
public:
string fileName;
std::ofstream& outputFile;
Report(string fileName, ofstream& outputFile) :
fileName{std::move(outputFile)},
outputFile{outputFile}
{
}
//creation of object:
std::ofstream ofs {"filename.txt}";
Report report {"filename.txt", ofs};
//you can use ofs from both here and from inside of report, but
//you have to ensure that ofs lives as long as report will use it or else you will enter Undefined Behaviour land
注意:如果您希望 class 成员和构造函数参数具有相同的名称,您需要像我一样使用 member initializer list。如果您决定使用引用,您也必须使用它。
我在声明继承 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
};
错误发生在倒数第 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.2.0\include\c++\fstream") cannot be referenced
-- it is a deleted function
是否不能创建包含ofstream
的构造函数?
错误也发生在第 9 行 outputFile = outputFile
。
谢谢。
不能拷贝传,不能拷贝一个,但是可以传引用,在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;
}
//...
};
在financialReport
中做同样的事情:
financialReport(string fileName, ofstream& outputFile) : Report(fileName, outputFile) {}
^
请注意,这是对问题中提出的问题的解决方案,正常情况下,但在更深入的分析中,虽然您没有详细说明您要实现的目标,但我不会这样做虽然可以说这是一种错误的方法,但您很有可能会以更好的方式构建您的程序。
是的,可以,但是错误提示您不能复制 std::ofstream
的对象。
根据你想做什么,有两种处理方式。
将 std::ofstream
的所有权传递给您新创建的对象:
Report(string fileName, ofstream outputFile) :
fileName{std::move(outputFile)},
outputFile{std::move(outputFile)}
{
}
//creation of object:
std::ofstream ofs {"filename.txt"};
Report report {"filename.txt", std::move(ofs)};
//ofs is empty here, it's whole content has been transferred to report object
传递对现有 std::ofstream
对象的引用:
class Report{
public:
string fileName;
std::ofstream& outputFile;
Report(string fileName, ofstream& outputFile) :
fileName{std::move(outputFile)},
outputFile{outputFile}
{
}
//creation of object:
std::ofstream ofs {"filename.txt}";
Report report {"filename.txt", ofs};
//you can use ofs from both here and from inside of report, but
//you have to ensure that ofs lives as long as report will use it or else you will enter Undefined Behaviour land
注意:如果您希望 class 成员和构造函数参数具有相同的名称,您需要像我一样使用 member initializer list。如果您决定使用引用,您也必须使用它。