2014-09-06 249 views
3

我得到這個錯誤,間接需要指針操作數('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; 
    } 
} 
+0

那是當我以爲我有它正確的:/ – MatthewTingle 2014-09-06 03:33:26

回答

2

你有

int empPtr = 0; 

其次

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

,這顯然是這個錯誤的原因。

+1

你會改變int empPtr到什麼程度?另外,你對我的數組聲明有什麼看法? – MatthewTingle 2014-09-06 03:22:45

+0

首先,你永遠不會使用'Employee empPtr = employeeArray [50];'的值,所以我不確定該行的目的是什麼。雖然你正在訪問超過數組末尾的索引('[50]'),但這是一個運行時錯誤,不會導致編譯器錯誤。其次'empPtr'不是一個指針,它只是一個'Employee'類型的棧變量。我不確定你在'readFromFile'中試圖做什麼,但是在這種情況下,你不能解引用非指針類型'int',這是'empPtr'在那個方法中的意思。 – mclaassen 2014-09-06 03:25:27

+0

我不想首先使用empPtr,但是被別人引用了它,我已經將上面的代碼改回到我之前在empPtr之前的版本。我的輸出文件打開,但沒有寫入它。 – MatthewTingle 2014-09-06 03:29:23

1

這條線是錯誤的。

Employee empPtr = employeeArray[50]; 

的最大有效指數employeeArray49

要獲得的陣列,使用的第一項:

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

+0

我應該改變它到什麼? – MatthewTingle 2014-09-06 03:17:31

+0

這不是一個運行時錯誤? – mclaassen 2014-09-06 03:22:25

+0

@mclaassen是的,這將是一個運行時錯誤。 – 2014-09-06 03:24:01