1

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&);
Matt
  • 14,906
  • 27
  • 99
  • 149
camdroid
  • 524
  • 7
  • 24
  • 1
    `T* x = new T()` is, like, never a good idea in C++. Why not just `Hand playerHand`? – leftaroundabout Nov 19 '12 at 03:19
  • `new T()` just sets a few default values for each of the objects, which wouldn't be done if I just called `Hand playerHand`. I guess I could set them manually, but it seemed easier to just use the pointers. Judging by the answers here, I might've been wrong, haha. – camdroid Nov 19 '12 at 03:25
  • 1
    If `Hand playerHand;` and `Hand* playerHand = new playerHand();` produce different objects, then you can fix that in your constructor by initializing your POD sub-objects. Otherwise you can do `Hand playerHand = Hand();` – Benjamin Lindley Nov 19 '12 at 03:46

2 Answers2

4

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Thanks, that seemed to work! I'm not familiar with smart pointers, and since this is for a class where performance isn't important (yet), this solution works just fine. But once I'm a bit more experienced with C++, I might come back and try to rewrite this code to be more efficient. Thanks for the tip! – camdroid Nov 19 '12 at 03:19
  • 2
    @camdroid it's not about efficiency, it's about writing good C++ code instead of C code in a C++ compiler. – Luchian Grigore Nov 19 '12 at 03:20
  • Oh, I see. Is there some resource I can use to see some common C++ standards? I'm more familiar with Java than C++, so I don't really know what good practices are with pointers. – camdroid Nov 19 '12 at 03:23
  • 1
    @camdroid http://stackoverflow.com/questions/tagged/c%2b%2b-faq and http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Luchian Grigore Nov 19 '12 at 03:24
  • Thanks very much - I'll try to check out some of those books in the list. – camdroid Nov 19 '12 at 03:27
1

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.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489