Lỗi không truy xuất được các thuộc tính private/protected trong lớp fstream

Mình có 2 đoạn code, về cơ bản là giống nhau, đoạn code thứ 1 dùng iostream, đoạn code thứ 2 dùng fstream thì bị lỗi này ?


Đoạn code 1:

#include<fstream>
#include<iostream>
#include<string>
using namespace std;

class Reactangle
{
protected:
    float a, b;
public:
    virtual void input(istream& inDevice)
    {
            cout << "Nhap chieu dai: ";
            inDevice >> a;
            cout << "Nhap chieu rong: ";
            inDevice >> b;
    }
};
class Square : public Reactangle
{
public:
    void input(istream& inDevice)
    {
            cout << "Nhap canh";
            inDevice >> a >> b;
    }
};
void main()
{
    Square *Test = new Square;
    Test->input(cin);
    system("pause");

Đoạn code 2:

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
#pragma once
class CCatalogue
{
protected:
    string m_ID;
    string m_Title;
    string m_Author;
    int m_Count;
public:
    /*int input(fstream f, int Pos) 
    {
            f.open("Input.txt", ios::in);
            f.seekg(Pos);
            
            f >> *this;
    }*/
    friend fstream& operator>>(fstream& f, CCatalogue &List);
    
};
fstream& operator>>(fstream& f, CCatalogue &List)
{
    
            //Read ID
    f >> List.m_ID;
            //Read title
    getline(f, List.m_Title);
            //Read author
    getline(f, List.m_Author);
            //Read amount of borrow
    f >> List.m_Count;
            
            return f;
}
class CBook : public CCatalogue
{
private:
    string m_Publisher;
    int m_Version;
    string m_Year;
public:
    int input(fstream f, int Pos)
    {
            f.open("Input.txt", ios::in);
            f.seekg(Pos);
            f >> *this;
    }
    friend fstream& operator>>(fstream& f, CBook &Book);
};

fstream& operator>>(fstream& f, CBook &Book)
{
    
    
            //Read ID
    f >> Book.m_ID;
            //Read title
    getline(f, Book.m_Title);
            //Read author
    getline(f, Book.m_Author);
            //Read amount of borrow
    f >> Book.m_Count;
            //Read the publisher
            getline(f, Book.m_Publisher);
            //Read the version
            f >> Book.m_Version;
            //Read the year
            getline(f, Book.m_Year);
    return f;
}
int main()
{
    fstream f;
    int Pos=0;
    CBook *Temp = new CBook();
    Temp->input(f, Pos);
}

đề bài đâu bạn, đọc code của bạn rối quá.:slight_smile:

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?