camdroid
camdroid

Reputation: 524

C++ References and Pointers

I have the following code in my program for a blackjack game:

Player *p;
Deck *d = new Deck();
Hand *playerHand = new Hand(), *dealerHand = new Hand();
p = get_Simple();  //This returns a pointer to an instance of a Simple Player for my game
Card dealerCard = d->deal();
p->draw(dealerCard, playerHand);

draw is defined as

virtual bool draw(Card dealer, const Hand &player);

When I try to run this code, I get the following error:

error: no matching function for call to 'Player::draw(Card&, Hand*&);
note: candidates are: virtual bool Player::draw(Card, const Hand&);

Upvotes: 1

Views: 175

Answers (2)

The error message is quite clear (after seeing it a couple of times):

error: no matching function for call to 'Player::draw(Card&, Hand*&); note: candidates are: virtual bool Player::draw(Card, const Hand&);

In the code you are trying to pass a Card and a Hand* to a function, but the compiler did not find an overload that takes those. The closest it got is a function that takes a Card and a const Hand&.

You need to dereference the pointer to get to the object:

p->draw(dealerCard, *playerHand);

Alternatively consider whether you need to hold all objects by pointer or not. Code is much simpler if you avoid pointers.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258548

The quick-fix would be to match the call with the signature:

p->draw(dealerCard, *playerHand);

The correct way would be to go about your code and get rid of dynamic allocation and raw pointers, replacing them with smart pointers.

Upvotes: 4

Related Questions