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

69,045

Solution 1

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.

Solution 2

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;
Share:
69,045
Larry
Author by

Larry

Updated on January 05, 2020

Comments

  • Larry
    Larry over 3 years

    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");
    }
    
  • Nate Hekman
    Nate Hekman about 10 years
    But c_info is a pointer to just one Employee, instantiated with a new just a few lines earlier. So that will compile but I don't think it's the right answer yet.
  • Larry
    Larry about 10 years
    Oh I get it now, very well explained. Thank you very much.
  • Larry
    Larry about 10 years
    Thank you very much to you both. That was fast :) everything works !!
  • leemes
    leemes about 10 years
    Try to use dynamic data-structures like std::vector, unless you have to use raw pointers / arrays due to school assignment requirements. In the high level C++ world it's more or less outdated (sometimes it's still used). std::vector is far more convenient, also regarding memory management issues (you can't simply forget to free the memory).
  • Larry
    Larry about 10 years
    Thanks for the suggestion, but I indeed have to use pointers for this assignment, but will try to make a vector version on my own for sure. Thanks !
  • leemes
    leemes about 10 years
    And you could also hand it in and suggest your professor to use vectors too ;) (unless this pointer-version assignment was just a low-level exercise, which is important too)
  • Larry
    Larry about 10 years
    it was a low level exercice. it is an introduction to programming class. we start seeing vector next semester :)