【问题标题】:Error with getline获取线错误
【发布时间】:2014-08-23 03:47:50
【问题描述】:

我在这里遇到了一个错误,不知道为什么,我对 c++ 很陌生,如果您可以查看我的其余代码,以确保一切正常,那就太好了。

这两行出现错误。

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

这是说类Employee没有成员First,我知道它不在那个函数中,我该如何解决它?

这是我的其余代码。

#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);
    }
}

【问题讨论】:

  • Employee 没有成员 firstPerson 有但不是Employee
  • 你会如何建议用我正在尝试做的事情来修复它?
  • 您需要对通过homeAddress 成员变量访问的Address 执行相同的操作。如果是Person,则需要添加.name 成员变量(例如:getline(in, e.name.first, ',');
  • 我不明白你怎么能正确地写下接下来的三行,但仍然不知道如何处理这两个错误的行。这是完全相同的事情,只是使用不同的成员。你写过这段代码吗?
  • 啊,非常感谢,我忘记了整个嵌套交易。

标签: c++ visual-c++ visual-studio-2012 getline


【解决方案1】:

我们如何将 Person 结构重命名为 Name 结构?
(毕竟它只包含 firstlast。)
这会给我们这个:

struct Name {
    string first;
    string last;
}; 

那么现在的员工是什么样子的呢?
它看起来像这样:

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

姓名Employee的一部分,但firstlast在哪里?它们是名称的一部分。
这是相同的图片,但它更深入地向您展示firstlast

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

您需要使用两个点运算符 ('.') 来访问 firstlast,因为它们的深度是两倍。

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

您的代码已经过重构以反映这些变化:

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 2017-03-01
    • 2012-12-19
    • 2020-01-22
    相关资源
    最近更新 更多