简体   繁体   中英

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&);

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM