C ++ expression must have an object pointer type

Dear stackoverflow team,

This is my first program using a pointer with a structure, and despite a lot of research, I could not find what I was looking for, but please forgive me if you already answered this.

I have a project for a school where I have to define structures, than use a pointer array to store data. In this loop, I get the error "Expression must be of type of pointer to an object"

for (int i = 0; i < nbClerk; i++) { cout<<"Number of hours: "; cin>>c_info->hoursWorked[i]; } break; 

here is all the code. Many thanks for your help

 #include <iostream> #include <string> #include <iomanip> using namespace std; //structure defining Employee struct Employee { int hoursWorked; int hourRate; double overtime; string name; int empID; }; //Function collecting and calculating info void employeeInfo(int nbOperator, int nbClerk){ char classOfEmployee; Employee *c_info; c_info = new (nothrow) Employee[nbClerk]; Employee *o_info; o_info = new (nothrow) Employee[nbOperator]; cout<<"Select the class of employee (C=Clerk, O=Operator)"; cin>>classOfEmployee; switch (tolower(classOfEmployee)) { case 'c': for (int i = 0; i < nbClerk; i++) { cout<<"Number of hours: "; cin>>c_info->hoursWorked[i]; } break; } } int main(){ int nb1,nb2; cout<<"Nb Employee and nb of nb of operator: "; cin>>nb1>>nb2; nbEmployee(nb1, nb2); system("pause"); } 
+4
source share
2 answers

You probably meant:

 c_info[i].hoursWorked; 

since c_info is an array, by doing c_info[i] , you get access to the i th instance (object) of the Employee class in the c_info array, and then get hoursWorked through . .

Now you can clearly see that your option simply does not make sense, since hoursWorked is just an integral type, not an array, and therefore you cannot apply the [] operator to it.

+6
source

c_info is a pointer to an Employee. You can assign one selected object to this pointer, or, in your case, several ( new with array syntax). Thus, he points to an array of employees.

You dereferenced this pointer. Since it points to an array of (several) employees, it also points to the first record. Then you access the integer member variable, which is still possible. But then you try to use the array index operator ( [] ) for an integer value, which is not possible.

You probably wanted to access the member variable of the i th record of your allocated array. So you need to rotate this: first use the array index operator, then refer to the member of that particular employee.

c_info[i] in low-level words means: take the pointer c_info , add i times the size of the specified type (so that it points to the i record) and dereference this address, This means that c_info[i] actually an Employee in the i th index (but not in the index).

Then you want to access the member of this employee. If it was still a pointer, you would need to use the arrow operator, but since you used the array index operator ( [i] ), you already dereferenced it, then the point operator is correct:

 cin >> c_info[i].hoursWorked; 
+2
source

All Articles