通过[]运算符c ++创建只能从文件中读取的函数

时间:2018-06-14 17:07:40

标签: c++98

我被要求创建一个新的类,在c ++中为fstream添加某些功能。 其中两个功能是[]运算符写入文件,第二个是只读文件。 类锁的头文件是这样的:

class RandomAccessFile{
     private:
        const ofstream writheFile; //Object for Writing:
        const ifstream readFile;   //Object for reading:
     public:
        RandomAccessFile(string fileName):writheFile(fileName), readFile(fileName){};
        inline const char& operator[](int num) const ; // read only from file by random access
       // some other functionality....
};

我尝试以这种方式实现只读操作符:

  const char& RandomAccessFile::operator[](int num) const {
    return readFile.get();
}

但我得到了错误:

no matching member function for call to 'get'

温我尝试相同的这个没有声明中的常量它可以正常工作 我该怎么做才能纠正这个问题? 感谢。

0 个答案:

没有答案