文件类
class File
{
private:
fstream dataFile;
public:
File();
};
File::File()
{
dataFile.open("Morse.bin", ios::in | ios::binary);
if(dataFile.fail())
cout << "File could not be opened.\n";
else
cout << "File opened successfully!\n";
}
解码器类
class Decoder: public File
{
private:
char line;
public:
void getLine();
};
void Decoder::getLine()
{
while(dataFile.get(line))
{
cout << line;
}
}
2个问题:
dataFile
是否包含Morse.bin
内容? file opened successfully
消息显示,但我只想确定。
我想在Decoder
课程中逐字逐句地阅读。我遇到的问题是从dataFile
课程访问Decoder
。我尝试为dataFile
创建一个访问器函数,但它不允许我访问它。错误消息是File::dataFile is inaccessible
。这是有道理的,因为它是私人的。但是,如果我无法创建一个将返回dataFile
的访问者函数,那么如何操纵dataFile
才能操纵它?
答案 0 :(得分:0)
使File
受到保护,或通过class File
{
protected:
fstream dataFile;
public:
File();
};
File::File()
{
dataFile.open("Morse.bin", ios::in | ios::binary);
if(dataFile.fail())
cout << "File could not be opened.\n";
else
cout << "File opened successfully!\n";
}
为其提供访问权限。
Na123