Indirection requires pointer operand
12,717
Solution 1
You have
int empPtr = 0;
followed by
(*empPtr).setEid(stoi(eidText));
which is clearly the cause of this error.
Solution 2
This line is wrong.
Employee empPtr = employeeArray[50];
The maximum valid index for employeeArray
is 49
.
To get the first item of the array, use:
Employee empPtr = employeeArray[0];
To get the last item of the array, use:
Employee empPtr = employeeArray[49];
More on accessing C and C++ arrays can be found at http://www.augustcouncil.com/~tgibson/tutorial/arr.html and http://www.tutorialspoint.com/cprogramming/c_arrays.htm.
Related videos on Youtube
Author by
MatthewTingle
Updated on June 30, 2022Comments
-
MatthewTingle almost 2 years
I'm getting this error, Indirection requires pointer operand ('int invalid') in this code, I think I'm using empPtr wrong in this code, but I'm not sure.
Thanks in advance guys.
I will also include my other classes at this link. https://gist.github.com/anonymous/08ff6c5284c179c9a323
My input text file looks like this.
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
Here is my 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; } }
-
MatthewTingle over 9 yearsWhat should I change it to?
-
MatthewTingle over 9 yearsWhat would you change int empPtr to? Also, what do you think about my array declarations?
-
mclaassen over 9 yearsFirst of all you are never using the value of
Employee empPtr = employeeArray[50];
, so I'm not sure what the purpose of that line is. Although though you are accessing an index past the end of the array ([50]
), that is a run time error and would not cause a compiler error. Second of allempPtr
is not a pointer, it is just a stack variable of typeEmployee
. I'm not sure what you are trying to do inreadFromFile
, but you cannot dereference a non pointer type,int
in this case, which is whatempPtr
is in that method. -
MatthewTingle over 9 yearsI've changed it to the [49] version, and I still get the error in my (*empPtr).setEid(stoi(eidText));
-
MatthewTingle over 9 yearsI didn't want to use empPtr in the first place, but was referred to it by someone else, I have changed the above code back to what I originally had before empPtr. My output file opens, yet nothing is being written to it.
-
mclaassen over 9 yearsSo are you still getting an error? I don't see
empPtr
at all anymore. -
MatthewTingle over 9 yearsI have removed empPtr, looking for other options why my output to file is not working, it opens the file but isn't writing for some odd reason.
-
mclaassen over 9 yearsAre you sure
employeeArray
is being populated with anything andemployeeCount
is not 0? -
MatthewTingle over 9 yearsYes I believe it is, when I look at empArray in the debugger it looks like everything is being populated. Maybe my temp is wrong? But I don't think that would have an effect.
-
MatthewTingle over 9 yearsIt actually says this line is messed up I believe empArray[temp].setEid(stoi(eidText)); in the debugger it says eidText is ""
-
mclaassen over 9 yearsWell there's your problem. All of your data is probably empty strings, so nothing is getting written to the file.
getline
is probably not working correctly. -
MatthewTingle over 9 yearsLet us continue this discussion in chat.
-
MarcusJ over 9 yearsI don't get it. can you access a variable as a variable some of the time and pointer other times, and if so how? would it change anything if I access it as a pointer all of the time?