c++ - 间接需要指针操作数

标签 c++ file-io outputstream pointers

我收到此错误,Indirection requires pointer operand ('int invalid') in this code,我想我在这段代码中错误地使用了 empPtr,但我不确定。

提前谢谢你们。

我还将在此链接中包括我的其他类(class)。 https://gist.github.com/anonymous/08ff6c5284c179c9a323

我的输入文本文件如下所示。

123,John,Brown,125 Prarie Street,Staunton,IL,62088
124,Matt,Larson,126 Hudson Road,Edwardsville,IL,62025
125,Joe,Baratta,1542 Elizabeth Road,Highland,IL,62088
126,Kristin,Killebrew,123 Prewitt Drive,Alton,IL,62026
127,Tyrone,Meyer,street,999 Orchard Lane,Livingston,62088

这是我的 main.cpp。

#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"

using namespace std;

bool openFileForReading(ifstream& fin, const string& filename);
bool openFileForWriting(ofstream& fout, const string& filename);

int readFromFile(ifstream& in, Employee empArray[]);

void writeToFile(ofstream& out, const Employee empArray[], const int numberofEmployees);

int main() {

    ifstream fin;
    ofstream fout;

    if(!openFileForReading(fin, "employeesIn.txt")) {
        cerr << "Error opening employeesIn.txt for reading." << endl;
        exit(1);
    }

    if(!openFileForWriting(fout, "employeesOut.txt")) {
        cerr << "Error opeing employeesOut.txt for writing." << endl;
        exit(1);
    }

    Employee employeeArray[50];

    int employeeCount = readFromFile(fin, employeeArray);

    fin.close();

    writeToFile(fout, employeeArray, employeeCount);

    fout.close();

    cout << "Program successful." << endl << endl;


    return 0;
}

bool openFileForReading(ifstream& fin, const string& filename) {
    fin.open("employeesIn.txt");

    return (fin.is_open());
}

bool openFileForWriting(ofstream& fout, const string& filename) {
    fout.open("employeesOut.txt");

    return (fout.is_open());
}

int readFromFile(ifstream& in, Employee empArray[]) {
    int temp = 0;
    string eidText;
    string first;
    string last;
    string street;
    string city;
    string state;
    string zipcode;

    while(!in.eof()) {
        getline(in, eidText, ',');
        getline(in, first, ',');
        getline(in, last, ',');
        getline(in, street, ',');
        getline(in, city, ',');
        getline(in, state, ',');
        getline(in, zipcode);

        empArray[temp].setEid(stoi(eidText));
        empArray[temp].setName(first, last);
        empArray[temp].setAddress(street, city, state, zipcode);

        temp++;
    }

    return temp;
}

void writeToFile(ofstream& out, const Employee empArray[], const int numberOfEmployees) {

    for (int i = 0; i < numberOfEmployees; i++){
        out << "Employee Record: " << empArray[i].getEid()
        << endl
        << "Name: " << empArray[i].getName()
        << endl
        << "Home Address: " << empArray[i].getAddress() 
        << endl
        << endl;
    }
}

最佳答案

你有

int empPtr = 0;

其次是

(*empPtr).setEid(stoi(eidText));

这显然是导致此错误的原因。

关于c++ - 间接需要指针操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25696422/

相关文章:

java - 如何使用 writeObject() 将 txt 导出到字符串中的路径?

linux - 二进制文件使用什么输出/输入流?

c# - 我应该为此创建一个单独的类吗?

c++ - 在生成数据的同时写入数据的最快方法

c++ - 在 C++ 中,如何获取一个程序的输出并将其用作另一个程序的输入?

c++ - 始终使用 {} 初始化对象是个好习惯吗?

c++ - 为什么双引用值在分配给 C++ 中的 float 变量时不会改变

c++ - 为什么我不能使用指向派生类指针的基类类型分配内存?

c++ - 复制另一个进程正在使用的文件

C:逐行读取文件