Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载运算符>>从流中读取数据_C++_Struct_Stream_Operator Overloading - Fatal编程技术网

C++ 重载运算符>>从流中读取数据

C++ 重载运算符>>从流中读取数据,c++,struct,stream,operator-overloading,C++,Struct,Stream,Operator Overloading,我试图重载操作符>>以从stream.txt文件读取Comp c的数据。除了第一个数字,即难度外,所有内容都读对了。它始终显示为0 以下是Comp的结构: struct Comp { string name; double points[7]; double difficulty; }; 这是重载>>的函数,它应该忽略.txt文件中的额外ws: istream& operator>>(istream& in, Comp& c)

我试图重载操作符>>以从stream.txt文件读取Comp c的数据。除了第一个数字,即难度外,所有内容都读对了。它始终显示为0

以下是Comp的结构:

struct Comp
{
    string name;
    double points[7];
    double difficulty;
};
这是重载>>的函数,它应该忽略.txt文件中的额外ws:

istream& operator>>(istream& in, Comp& c)
    {
        getline(in, c.name);
        in >> ws >> c.difficulty >> ws >> c.points[0] >> ws >> c.points[1] >> ws >> c.points[2] >> c.points[3] >> ws >> c.points[4] >> ws >> c.points[5] >> ws >> c.points[6] >> ws >> c.points[7] >> ws;
        cout << c.name << endl;
        cout << c.difficulty << endl;

        return in;
    }
为什么第一个数字读错了!0


如果我遗漏了任何其他有用的信息,请告诉我。

c.points[7]已过期range@DieterL对不起,什么意思?Comp::points数组只能容纳7个元素,因此访问c.points[7]无效,超出范围。@bialpio是的,当然。谢谢你们。
int read_competitors(istream& in, Comp V[], int n)
{
    int counter = 0;

    while (counter < n && (in >> V[counter])) {
        ++counter;
    }

    return counter;
}
Anna Lindberg
2.3 6.8 7.2 6.4 8.1 2.5 6.2 7.0 
Test Name
2.3 3.4 5.6 7.8 8.5 5.4 3.6 4.7
   First Last
2.2 7.8 7.5 8.7 7.2 9.2 7.5 4.5 
Asdf Tyui
1.9 7.8 5.6 3.4 3.8 5.1 5.9 7.2
Jio Wkfjf
2.4 4.0 4.2 5.1 4.3 7.7 4.9