Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
}
like image 253
Larry Avatar asked Apr 18 '13 19:04

Larry


1 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.

like image 152
Alexander Shukaev Avatar answered Sep 28 '22 06:09

Alexander Shukaev