C++ Fstream作为私有成员访问

C++ Fstream作为私有成员访问,c++,inheritance,fstream,C++,Inheritance,Fstream,文件类 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 <<

文件类

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;
    }
}
类文件
{
私人:
流数据文件;
公众:
文件();
};
File::File()文件
{
打开(“Morse.bin”,ios::in | ios::binary);
if(dataFile.fail())
库特
  • 还没有,你还没读过
  • 使
    数据文件
    受保护,或从
    文件
    向其提供访问器

    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";
    }
    
    类文件
    {
    受保护的:
    流数据文件;
    公众:
    文件();
    };
    File::File()文件
    {
    打开(“Morse.bin”,ios::in | ios::binary);
    if(dataFile.fail())
    库特
    
  • 还没有,你还没读过
  • 使
    数据文件
    受保护,或从
    文件
    向其提供访问器

    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";
    }
    
    类文件
    {
    受保护的:
    流数据文件;
    公众:
    文件();
    };
    File::File()文件
    {
    打开(“Morse.bin”,ios::in | ios::binary);
    if(dataFile.fail())
    
    cout
    fstream getDataFile(){return dataFile;}
    给了我一个错误,
    数据文件
    不可访问。@Bryan你是在
    解码器
    类中添加的吗?如果是这样,你需要将它放在
    文件
    类中。私有成员只能在声明它的类中访问。甚至继承类都不能访问它。我正在将它添加到fi的公共部分le
    fstream getDataFile(){return dataFile;}
    给了我一个错误,
    数据文件
    不可访问。@Bryan你是在
    解码器
    类中添加的吗?如果是这样,你需要将它放在
    文件
    类中。私有成员只能在声明它的类中访问。甚至继承类都不能访问它。我正在将它添加到fi的公共部分leYou可以在protected中添加数据文件,也可以将
    解码器
    定义为friend类。您有第二个选项,但通常不建议这样做。您可以在protected中添加数据文件,或者可以将
    解码器
    定义为friend类。您有第二个选项,但通常不建议这样做