gpt4 book ai didi

c++ - 错误:无法声明变量[变量名称]为抽象类型[子类]

转载 作者:行者123 更新时间:2023-12-01 14:53:16 25 4
gpt4 key购买 nike

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

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

请有人帮助我了解此错误的来源,因为我对c++不熟悉。谢谢!

这是给我问题的代码:

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;
};

然后,我尝试创建一个SamplePlayer,如下所示:
 SamplePlayer player("","");

我不理解此错误,因为SamplePlayer是Player的子类,而Player是抽象类。

最佳答案

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

然后,您的派生类SamplePlayer仅为其中一些提供了实现,而为其他人(而不是为DDL()方法提供)提供了实现,因此您的派生类为,还为是无法实例化的抽象类。

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

(而且不,看到的错误是,运行时没有得到。尝试编译时得到。必须首先成功编译才能尝试运行。)

关于c++ - 错误:无法声明变量[变量名称]为抽象类型[子类],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60787564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com