简体   繁体   English

getline错误

[英]Error with getline

I'm getting an error here, and am not sure why, I'm very new to c++ and if you could look at the rest of my code, to make sure it is alright that would be great. 我在这里遇到错误,并且不确定为什么,我对c ++还是很陌生,如果您可以看一下我其余的代码,请确保它很好。

I'm getting an error on these two lines. 我在这两行上遇到错误。

getline(in, e.first, ',');
getline(in, e.last, ',');

It's saying class Employee has no member First, and I know that it's not in that function, how can I fix it? 就是说Employee类没有成员First,而且我知道它不在该函数中,我该如何解决呢?

Here is the rest of my code. 这是我其余的代码。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Person {
    string first;
    string last;
};

struct Address {
    string street;
    string city;
    string state;
    string zipcode;
};

struct Employee {
    Person name;
    Address homeAddress;
    int eid;
};

void readEmployee(istream& in, Employee& e);
void displayEmployee(ostream& out, const Employee& e);

int main(int argc, const char* argv[]) {
    Employee e[50];

    ifstream fin;
    ofstream fout;

    fin.open("employeesIn.txt");

    if (!fin.is_open()) {
        cerr << "Error opening employeesIn.txt for reading." << endl;
        exit(1);
    }
    fout.open("employeesOut.txt");
    if (!fout.is_open()) {
        cerr << "Error opening employeesOut.txt for writing." << endl;
        exit(1);
    }
    int EmployeePopulation = 0;
    readEmployee(fin, e[EmployeePopulation]);
    while (!fin.eof()) {
        EmployeePopulation++;
        readEmployee(fin, e[EmployeePopulation]);
    }
    fin.close();
    for (int i = 0; i <= EmployeePopulation - 1; i++) {
        displayEmployee(fout, e[i]);
    }

    fout.close();

    cout << endl;

    return 0;
}

void readEmployee(istream& in, Employee& e) {
    string cidText;
    if (getline(in, cidText, ',')) {
        e.eid = stoi(cidText);

        getline(in, e.first, ',');
        getline(in, e.last, ',');

        getline(in, e.homeAddress.street, ',');
        getline(in, e.homeAddress.city, ',');
        getline(in, e.homeAddress.state, ',');

        string zipcodeText;
        getline(in, zipcodeText, ',');
        e.homeAddress.zipcode = stoi(zipcodeText);
    }
}

How about we rename the Person struct to be a Name struct instead? 我们如何将Person结构重命名为Name结构呢?
(It only contains a first and last after all.) (毕竟只包含第一个最后一个 。)
That would give us this: 那会给我们这个:

struct Name {
    string first;
    string last;
}; 

So what does an Employee look like now? 那么,员工现在是什么样子?
It looks like this: 看起来像这样:

 +-------------+ | Employee | | | | +---------+ | | | Name | | | +---------+ | | | | +---------+ | | | Address | | | +---------+ | | | +-------------+ 

Name is a part of Employee , but where is first and last ? 名称员工的一部分,但如果是第一个最后一个 They are a part of Name . 它们是Name的一部分。
Here is the same picture, except it goes a layer deeper to show you first and last : 这是同一张图片,除了它更深入地展示了您的第一个最后一个

+---------------+
|   Employee    |
|               |
| +-----------+ |
| |   Name    | |
| |           | |
| | +-------+ | |
| | | first | | |
| | +-------+ | |
| |           | |
| | +-------+ | |
| | | last  | | |
| | +-------+ | |
| |           | |
| +-----------+ |
|               |
|  +---------+  |
|  | Address |  |
|  +---------+  |
|               |
+---------------+

You need to use TWO dot operators ('.'), to access first and last , because they are TWICE as deep. 您需要使用两个点运算符('。')来访问firstlast ,因为它们的深度是TWICE的两倍。

Employee e;
e.name.first = "Joe";

Your code as been re-factored to reflect these changes: 您的代码经过重构以反映这些更改:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Name {
    string first;
    string last;
};

struct Address {
    string street;
    string city;
    string state;
    string zipcode;
};

struct Employee {
    Name name;
    Address homeAddress;
    int eid;
};

void readEmployee(istream& in, Employee& e);
void displayEmployee(ostream& out, const Employee& e);

int main(int argc, const char* argv[]) {
    Employee e[50];

    ifstream fin;
    ofstream fout;

    fin.open("employeesIn.txt");

    if (!fin.is_open()) {
        cerr << "Error opening employeesIn.txt for reading." << endl;
        exit(1);
    }
    fout.open("employeesOut.txt");
    if (!fout.is_open()) {
        cerr << "Error opening employeesOut.txt for writing." << endl;
        exit(1);
    }
    int EmployeePopulation = 0;
    readEmployee(fin, e[EmployeePopulation]);
    while (!fin.eof()) {
        EmployeePopulation++;
        readEmployee(fin, e[EmployeePopulation]);
    }
    fin.close();
    for (int i = 0; i <= EmployeePopulation - 1; i++) {
        displayEmployee(fout, e[i]);
    }

    fout.close();

    cout << endl;

    return 0;
}

void readEmployee(istream& in, Employee& e) {
    string cidText;
    if (getline(in, cidText, ',')) {
        e.eid = stoi(cidText);

        getline(in, e.name.first, ',');
        getline(in, e.name.last, ',');

        getline(in, e.homeAddress.street, ',');
        getline(in, e.homeAddress.city, ',');
        getline(in, e.homeAddress.state, ',');

        string zipcodeText;
        getline(in, zipcodeText, ',');
        e.homeAddress.zipcode = stoi(zipcodeText);
    }
}
void displayEmployee(ostream& out, const Employee& e){

  //you can access the specific name values this way:
  cout <<  e.name.first << " " << e.name.last << endl;

  return;
}

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

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