间接需要指针操作数

时间:2014-09-06 03:12:33

标签: c++ file-io outputstream pointers

我收到此错误,Indirection需要在此代码中使用指针操作数(' int invalid'),我认为我在此代码中使用了empPtr错误,但我'我不确定。

先谢谢你们。

我还将在此链接中包含我的其他课程。 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;
    }
}

2 个答案:

答案 0 :(得分:2)

你有

int empPtr = 0;

接着是

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

这显然是造成此错误的原因。

答案 1 :(得分:1)

这条线错了。

Employee empPtr = employeeArray[50];

employeeArray的最大有效索引为49

要获取数组的第一项,请使用:

Employee empPtr = employeeArray[0];

要获取数组的最后一项,请使用:

Employee empPtr = employeeArray[49];

有关访问C和C ++数组的更多信息,请访问http://www.augustcouncil.com/~tgibson/tutorial/arr.htmlhttp://www.tutorialspoint.com/cprogramming/c_arrays.htm