I am trying to create a blackjack simulation.
There are two Player subclasses (SimplePlayer and CountingPlayer), derived from an abstract base class Player.
One of the methods is shared between the two subclasses, so I am trying to implement it in the manner of 'bool Player::Method(struct x, struct &y)' before the subclass implementations in the source file so that the subclasses inherit it, but if I omit declarations of this method from the two subclasses, I get the error:
cannot declare variable ‘impl’ to be of abstract type ‘CountingPlayer’ because the subclass becomes abstract
But if I include the declaration ('bool Method(struct x, struct y)') in the subclass implementation, I get the 'undefined reference to vtable' error.
How do I make the method inheritable by the subclasses so each subclass doesn't repeat the same implementation?
I cannot modify my header file, but the pure virtual method declared there is public.
#ifndef __PLAYER_H_
#define __PLAYER_H_
#include "hand.h"
class Player {
// A virtual base class, providing the player interface
public:
virtualint bet(unsignedint bankroll,
unsignedint minimum) = 0;
// REQUIRES: bankroll >= minimum
// EFFECTS: returns the player's bet, between minimum and bankroll
// inclusive
virtualbool draw(Card dealer, // Dealer's "up card"
const Hand &player) = 0; // Player's current hand
// EFFECTS: returns true if the player wishes to be dealt another
// card, false otherwise.
virtualvoid expose(Card c) = 0;
// EFFECTS: allows the player to "see" the newly-exposed card c.
// For example, each card that is dealt "face up" is expose()d.
// Likewise, if the dealer must show his "hole card", it is also
// expose()d. Note: not all cards dealt are expose()d---if the
// player goes over 21 or is dealt a natural 21, the dealer need
// not expose his hole card.
virtualvoid shuffled() = 0;
// EFFECTS: tells the player that the deck has been re-shuffled.
virtual ~Player() { }
// Note: this is here only to suppress a compiler warning.
// Destructors are not needed for this project.
};
extern Player *get_Simple();
// EFFECTS: returns a pointer to a "simple player", as defined by the
// project specification
extern Player *get_Counting();
// EFFECTS: returns a pointer to a "counting player", as defined by
// the project specification.
#endif /* __PLAYER_H__ */
Are you limited to the two subclasses only in the hierarchy? Meaning, could you subclass your abstract class with a 'concrete Player' class, implement methods that have same behavior for the other two subclasses then derive you two subclasses from the concrete class?