Matthew Hibberd
Matthew Hibberd

Reputation: 23

Error: cannot declare variable [variable name] to be of abstract type [Sub Class]

I am currently editing source code but when I try to compile it I get:

error: cannot declare variable ‘player’ to be of abstract type ‘SamplePlayer’
     SamplePlayer player("","");

Please could someone help me understand where this error is coming from, I am not familiar with c++. Thank you!

This is the code that is giving me issues:

SamplePlayer.h:

#include "../jni/Player.h"

class SamplePlayer : public Player
{
public:
  SamplePlayer(const std::string &playerType, const std::string &params);  
  public: ~SamplePlayer();

  void gameChange(const Wrapper::SimpleGame &sg, int move_index=-1);
  std::string computeMove(const Wrapper::SimpleGame &sg, double time);
  void reset();
  void interrupt();
};

Player.h:

class Player
{
public:

  Player()
  {
    interrupted = false;
  }

  // signal move computation to exit
  virtual void interruptMoveComputation() { interrupted = true; }

  // reset game
  virtual void reset() = 0;

  /** inform player about game state changes
      @param move_index = -1: last move in sequence, otherwise that move (for easy replay)
  */
  virtual void gameChange(const Wrapper::SimpleGame &game, int move_index = -1) = 0;

  // return move in current game state
  virtual std::string computeMove(const Wrapper::SimpleGame &game, double time) = 0;


  /** DDS C++ interface (C++ is 3+ times faster)

      trump-games:

      return card point *differential* in view of player to move
      given window (alpha,beta)

      null-games:

      return > 0 if ptm wins, < 0 otherwise

      leaf values = +/- (100 + # declarer cards)
  */

  virtual int DDS(int fh, int mh, int rh,            // remaining cards (bit sets)
                  int toMove,                        // 0,1,2
                  int declarer,                      // 0,1,2
                  int gameType,        // 0,1,2,3 (diamonds..clubs), 4 (grand), 5 (null)
                  int trickCardNum,                  // number of cards in trick (0,1,2)
                  int trickCard1, int trickCard2,    // trick card indexes
                  int ptsDeclarer, int ptsDefenders, // card points thus far -
                                                     // including skat (0..120)
                  int alpha, int beta,               // point *differential* bounds for ptm
                                                     // (ignored in null-games)
                  int clearHash,                     // !=0: clear hash table before searching
                  int paranoid                       // 0: not paranoid, 2: unknown skat
                  ) = 0;

  /** Paranoid C++ interface */

  virtual void paranoid_init(const char *file,  // table filename
                             int hand           // 10-card hand
                             ) = 0;
  /*
    return paranoid value:
    0: win
    1: schneider
    2: schwarz
    3: loss
  */
  virtual int paranoid(int hand,     // hand bit set
                       int skat,     // skat bit set
                       int gameType  // 0,1,2,3 (diamonds..clubs), 4 (grand)
                       ) = 0;


  // paranoid search on a set of worlds
  virtual int w_paranoid(int declHand,                      // declarer's hand
                         int played,                        // all played moves
                         int toMove,                        // 0,1,2
                         int declarer,                      // 0,1,2
                         int gameType,                      // 0,1,2,3 (diamonds..clubs), 4 (grand), 5 (null - not implemented)
                         int trickCardNum,                  // number of cards in trick (0,1,2)
                         int trickCard1, int trickCard2,    // trick card indexes
                         int ptsDeclarer, int ptsDefenders, // card points thus far - including skat (0..120)
                         int alpha, int beta,               // point *differential* bounds for ptm, ignored in null
                         int clearHash,                     // !=0: clear hash table before searching
                         // worlds (all vectors same size)
                         int n,                             // number of worlds
                         int *hands1,                       // n tomove+1's hands
                         int *hands2,                       // n tomove+2's hands
                         int *skats                         // n skats
                         ) = 0;

  virtual ~Player() { }

protected:

  bool isInterrupted() { return interrupted; }

  bool interrupted;
};

Then I try to create a SamplePlayer like this:

 SamplePlayer player("","");

I don't understand this error because SamplePlayer is the sub class of Player, and Player is the abstract class.

Upvotes: 2

Views: 1009

Answers (1)

Mike Nakis
Mike Nakis

Reputation: 62045

Your base class Player declares several pure virtual methods, (virtual method() = 0,) and as such, it is an abstract class which cannot be instantiated.

Then your derived class SamplePlayer provides implementations only for some of them, but not for others, (for example, not for method DDL(),) so your derived class is also an abstract class which may not be instantiated.

To solve this, you are going to need to provide an implementation in SamplePlayer for each pure virtual method declared in Player.

(And no, that error you see, you do not get it when you run. You get it when you try to compile. You will have to first successfully compile before you can try running.)

Upvotes: 1

Related Questions