1

I come with a perhaps odd question. I was doing an exercise and I ran into a problem. The point was to make an Employee class, and then a function that has an array of Employee** and its size as an argument, and to make it show every employee with more than 5 years of experience.

Here is the relevant pieces of the Employee class:

//in Employee.h:

int Getexperience() { return experience; }
virtual void show();


//in Employee.cpp:
void Employee::show()
{
    cout << "Name: " << this->surname << ", Age: " << this->age << ", Experience: " << this->experience << ", Salary: " << this->salary << endl;
}

And here's the function, written in main.cpp:

void whoWorkMoreThan5Years(Employee** tab[], int size){
    for(int i =0; i<size; i++){
        if(tab[i]->Getexperience() > 5){
            tab[i]->show();
        }
    }
}

This gets me the error:

error: request for member 'Getexperience' in '* *(tab + ((sizetype)(((unsigned int)i) * 4u)))', which is of pointer type 'Employee*' (maybe you meant to use '->' ?)|

And I honestly have no clue what it's even supposed to mean since

  1. I am using a '->' for the object's method.
  2. I don't know what the '* *(tab + ((sizetype)(((unsigned int)i) * 4u)))' part even refers to.

I guess my question is basically: what am I doing wrong here? Is there any special way to use an object's methods in this situation that I just don't know?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    By throwing that [] in there you’ve created a employee *** – Taekahn Jan 05 '22 at 22:13
  • 1
    I think you've wandered into [X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) territory and should expand on why you want three dimensions of `Employee` Could be you've misinterpreted something in the assignment. – user4581301 Jan 05 '22 at 22:17
  • I suspect you've made a wrong choice in having your function accept an argument that is an array of pointers to pointers (which is equivalent to accepting an `Employee***`). The cause of the error is that `tab[i]` is of type `Employee**`, and a call of the form `thing->show()` requires `thing` to be of type `Employee *` (note the number of asterisks). One way (to get the types right, and shut the compiler up, at least) would be `(*tab[i])->show()`. Unless the caller has set things up, however, just doing that will cause undefined behaviour (hence my opening observation about the bad choice) – Peter Jan 05 '22 at 23:58

1 Answers1

1

The way you are passing in the array is wrong. Currently Employee** tab[] means that you have an array of pointers to a pointer to an Employee object. What you want to have is either Employee* tab[] or Employee** tab which are both arrays of pointers to an Employee object.

Note that Employee** is the same as Employee* tab[] because arrays decay into pointers.

marco
  • 341
  • 1
  • 7
  • 1
    This isn’t quite correct: *inside a function argument list*, `T[]` isn’t an array, it’s a pointer. That is, it’s *identical* to `T*`. This is different from variable declarations. Therefore, `Employee** tab[]` is *identical* to `Employee***`, and this is never an array: when an array of appropriate type is passed into this parameter, it decays before the function call. – Konrad Rudolph Jan 05 '22 at 22:44
  • That's what I had initially, but I edited my post to fit another commenter's response that linked me this article to link in my answer. – marco Jan 05 '22 at 22:46
  • Unfortunate victim of history, that `Employee***` does not convey which of those pointers are arrays and which are single instance. (And in the one situation, a single instance and an array of but a single element look a lot alike when looking down the barrel of a pointer.) – Eljay Jan 05 '22 at 23:09
  • The initial version stated that arrays were pointers, which is not true. I thought about pointing out that in the edit there were no arrays because they had already decayed, but figured you'd gotten close enough with the edit. – user4581301 Jan 06 '22 at 01:38
  • fair enough. Thanks all for the clarification! – marco Jan 06 '22 at 05:08
  • Ah, yep, that helped, once I changed it to just `Employe** tab` it started working. It came down to me not understanding how pointers to a pointer work exactly, I didn't know they were like arrays in and of themselves. Thank you for clarifying! – NeonArmageddon Jan 06 '22 at 15:21