user2105982
user2105982

Reputation: 187

Calling a class function with pointers issue

I am getting this error with my code.

error C2228: left of '.showHand' must have class/struct/union 1>type is 'PokerHand *' did you intend to use '->' instead?

This is the part of my main function that is getting the error. The error thing is the one where I call showHand()

PokerHand* Dog = Wood.dealN(5);
Dog.showHand();

I'm guessing I'm doing something incorrectly with pointers which is why the error is telling me to use ->, however I'm not sure where I would even put this in my code. I just need to be able to call the showHand() function in my main file, as far as I know every other part of the code is working.

Upvotes: 0

Views: 74

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595827

Use the . operator when the left-hand side is a direct object instance.

Use the -> operator when the left-hand side is a pointer to an object instance.

The -> operator is a combination of the * and . operators. In other words, this code:

Dog->showHand();

Is the same as this code:

(*Dog).showHand();

Because the * operator first dereferences the pointer to gain access to the object instance, and then the . operator can access the object's members.

Upvotes: 1

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158469

This line:

Dog.showHand();

Should be:

Dog->showHand();

Since it is a pointer you can not use . to access the members you must use ->.

Upvotes: 4

Related Questions