简体   繁体   English

在我的代码中“不匹配'operator >>'”是什么意思?

[英]What does “no match for 'operator >>'” mean in my code?

I'm trying to look for what is wrong in this fragment of code. 我正在尝试寻找这段代码中的错误。 It says the error : [Error] no match for 'operator>>' in 'inputData >> Player[i].AthleteType::firstName ' for the line: 它显示错误: [Error] no match for 'operator>>' in 'inputData >> Player[i].AthleteType::firstName ' [Error] no match for 'operator>>' in 'inputData >> Player[i].AthleteType::firstName

inputData >> Player[i].firstName;

Can someone tell me what this means? 有人可以告诉我这是什么意思吗? And also if this is the right way to read data from a file that looks like this: 同样,如果这是从看起来像这样的文件中读取数据的正确方法,则:

Peter Gab 2653 Kenya 127
Usian Bolt 6534 Jamaica 128
Other Name 2973 Bangladesh -1
Bla Bla 5182 India 129
Some Name 7612 London -1



//this is the structure
struct AthleteType
{
    string firstName[SIZE];
    string lastName[SIZE];
    int athleteNumber[SIZE];
    string country[SIZE];
    int athleteTime[SIZE];
};


void readInput(int SIZE)
{
    AthleteType Player[SIZE];

    ifstream inputData("Athlete info.txt");

    int noOfRecords=0;

    for (int i=0; i < SIZE; i++, noOfRecords++)
    {
        inputData >> Player[i].firstName; 
        inputData >> Player[i].lastName;
        inputData >> Player[i].athleteNumber;
        inputData >> Player[i].country;
        inputData >> Player[i].athleteTime;
    }

    for (int i=0; i < noOfRecords; i++)
    {
        cout << "First Name: " << Player[i].firstName << endl;
        cout << "Last Name: " << Player[i].lastName << endl;
        cout << "Athlete Number: " << Player[i].athleteNumber << endl;
        cout << "Country: " << Player[i].country << endl;
        cout << "Athlete Time: " << Player[i].athleteTime << endl;
        cout << endl;
    }
}

There are several problems with your attempt. 您的尝试有几个问题。 Firstly your struct 首先你的结构

struct AthleteType {
    string firstName[SIZE];
    string lastName[SIZE];
    int athleteNumber[SIZE];
    string country[SIZE];
    int athleteTime[SIZE]; 
};

Your compiler error is telling you that you can't read into an array of strings, inputData >> firstName[SIZE];. 编译器错误告诉您无法读入字符串数组inputData >> firstName [SIZE];。 One string at a time is fine of course. 一次只需要一个字符串就可以了。

If i peer into my crystal ball, I see that you want to store several athletes. 如果我凝视我的水晶球,我会发现您想存储几个运动员。 This should be done using a vector. 这应该使用向量来完成。

vector<Athlete> athletes;

And the struct can then be 然后该结构可以是

struct Athlete
{
    string firstName;
    string lastName;
    int athleteNumber;
    string country;
    int athleteTime;
};

One athlete per object. 每个物件一名运​​动员。

When reading from an input file you want to read based on read success. 从输入文件读取时,要基于读取成功来读取。

   while(inputData >> athlete){  
       athletes.push_back(athlete); 
   }

You can do this by overloading operator>> (istream&, Athlete& ); 您可以通过重载operator>> (istream&, Athlete& );来完成此operator>> (istream&, Athlete& ); or you can write a function that does a similar job. 或者您可以编写执行类似工作的函数。

istream& readAthlete(istream& in, Athlete& at){
    return in >> at.firstName >> at.lastName >> at.athleteNumber >> ... and so on;
}

Now the read function can be written as 现在读取功能可以写成

vector<Athlete> readInput(string filename){
    vector<Athlete> athletes;
    ifstream inputData(filename);
    Athlete athlete;
    while(readAthlete(inputData, athlete)){
        athletes.push_back(athlete);
    }
    return athletes;
}

This is not tested code, it might work, it might not work, but it should give you a reasonable path forward. 这不是经过测试的代码,它可能会起作用,也可能不会起作用,但是它应该为您提供一条合理的前进道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM