C ++,得到一个"没有运算符">>"匹配这些操作数"。对于读取文件的函数

c ++ , getting a 'no operator ">>" matches these operands'. For a function that reads a file

本文关键字:gt 操作数 于读取 函数 文件 读取 一个 运算符      更新时间:2023-10-16

我在这个代码中有错误:在"in>>point"中">>"的while循环中

void FileRead(const int cap,const string point,int counter)
{
    ifstream in;
    in.open("sample_strings.txt"); //ifstream in;  in.open("sample_data.txt");
    if (in.fail())
        cout<<"sample_data not opened correctly"<<endl;
    while(!in.eof() && counter<cap)
    {
        in>>point[counter];
        counter++;
    }
    in.close();
}

我尝试实现我需要>>运算符,但仍然收到错误。

ifstream & operator>>(ifstream & in, ARRAY & Original)
{
    cout<<"operator>> has been calledn";
    Original.counter = 0;
    while(!in.eof() && Original.count<Original.cap)
    {
        in>>Original. point[Original.counter];
        (Original.counter)++;
    }
    return in;
}

我正在实现读取文件函数以在传递构造函数时读取构造函数的数组

我拥有的私人功能是这些

private:
string *point;
int counter;
int cap;

我仍然是 c++ 的新手,所以任何理解这一点的小帮助都会有所帮助。

您正在尝试读入const string。不是传递const string point,而是传递string& point作为对字符串的可修改引用传递。

线路in>>point[counter]point上调用operator[],然后调用operator>>。但是因为point是一个const stringoperator[]返回一个不应该读入的const char&。你希望它返回char&,所以point必须是非常量。