C++ Expression must have pointer-to-object type

Dear stackoverflow helpful community,

This is my first program using a pointer with a structure, and despite lots of research, I was not able to find what I was looking for. Please forgive me if this has already been responded to.

I have a project for school where I have to define structures than use pointers array to store data. In this loop, I get the following error :

Expression must have pointer-to-object type

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

here’s the whole code. thank you very much 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");
}

2 Answers

You probably meant:

c_info[i].hoursWorked;

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

Now you can clearly see that your variant simply doesn’t make sense, as hoursWorked is just an integral type and not an array, and therefore you cannot apply [] operator to it.

c_info is a pointer to an Employee. You can assign a single allocated object to such a pointer or, in your case, multiple ones (new with the array syntax). So it points to an array of Employees.

You dereferenced that pointer. Since it points to an array of (multiple) Employees, it also points to the first entry. Then you access an integer member variable, which is still possible. But then you try to use the array subscript operator ([]) on an integer value, which is not possible.

You probably meant to access the member variable of the i-th entry of your allocated array. So you have to turn this around: First use the array subscript operator, then access the member on that particular Employee.

c_info[i] in low-level words means: Take the pointer c_info, add i times the size of the type it points to (so it points to the i-th entry) and dereference that address. This means, that c_info[i] actually is the Employee at the i-th index (but not a pointer).

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

cin >> c_info[i].hoursWorked;

Leave a Reply

Your email address will not be published. Required fields are marked *