首页 > 解决方案 > 错误:无法将变量 [变量名称] 声明为抽象类型 [子类]

问题描述

我目前正在编辑源代码,但是当我尝试编译它时,我得到:

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

请有人帮我理解这个错误来自哪里,我不熟悉 C++。谢谢!

这是给我问题的代码:

样品播放器.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();
};

播放器.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;
};

然后我尝试像这样创建一个 SamplePlayer:

 SamplePlayer player("","");

我不明白这个错误,因为 SamplePlayer 是 Player 的子类,而 Player 是抽象类。

标签: c++

解决方案


您的基类Player声明了几个纯虚拟方法 ( virtual method() = 0,),因此,它是一个无法实例化的抽象类。

然后你的派生类SamplePlayer只为其中一些提供实现,但不为其他人提供实现,(例如,不为 method DDL(),)所以你的派生类也是一个抽象类,可能不会被实例化。

为了解决这个问题,您将需要为在SamplePlayer中声明的每个纯虚拟方法提供一个实现Player

(不,你看到的那个错误,你在运行时没有得到它。你在尝试编译时得到它。你必须先成功编译才能尝试运行。)


推荐阅读