c++ - 用“虚拟”类继承
问题描述
我目前正在尝试编写一个通用的 minmax 算法。我对 c++ 相当陌生,具有 c 和 arm asm 编程背景。对于 MinMax-Algorithm,我使用了一个称为 Negamax 的变体,这使得它更加紧凑:
#define move typename game<evaluationtype>::_move
#define gamestate typename game<evaluationtype>::_gamestate
#define player typename game<evaluationtype>::_player
template <typename gametype, typename evaluationtype>
std::pair<evaluationtype, move> Negamax(game <evaluationtype> Game, int depth, evaluationtype alpha, evaluationtype beta) {
if (depth == 0)
return std::make_pair(Game.evaluation(), move());
else {
move savebestmove;
evaluationtype maxValue = alpha;
std::list<move> allpossiblemoves = Game.getpossiblemoves();
for (move Move : allpossiblemoves) {
gamestate state = Game.makemove(Move);
evaluationtype checkvalue = -((Negamax<gametype, evaluationtype>(Game, depth - 1, -beta, -maxValue)).first);
Game.undomove(Move, state);
if (checkvalue > maxValue) {
maxValue = checkvalue;
savebestmove = Move;
if (maxValue >= beta)
break;
}
}
return std::make_pair(maxValue, savebestmove);
}
}
#undef move
#undef gamestate
#undef player
在代码中设置游戏基类所需的成员函数,如下所示:
template <typename evaluationtype>
class game {
enum class _player;
class _move;
class _gamestate;
private:
_player playersturn;
public:
game() = delete;
virtual evaluationtype evaluation() const;
virtual std::list<_move>&& getpossiblemoves() const;
virtual _gamestate makemove(const _move&);
virtual void undomove(const _move&, const _gamestate&);
};
现在我尝试用井字游戏实现派生类:
template <typename evaluationtype>
class TicTacToe : game<evaluationtype> {
private:
enum class _player {
none, cross, circle
};
class _move {
unsigned x;
unsigned y;
};
class _gamestate {
std::array<std::array<_player, 3> ,3> field;
_player turn = cross;
};
_gamestate currentfield;
public:
TicTacToe();
evaluationtype evaluation() const;
std::list<_move>&& getpossiblemoves() const;
_gamestate makemove(const _move&);
void undomove(const _move&, const _gamestate&);
};
当前唯一具有定义的方法是一个空的构造函数。Visual Studio 没有向我显示任何错误,但是一旦我尝试创建 TicTacToe 的实例,我就会得到以下输出:
1>C:\AlphaBetaNegamax\TicTacToe.h(17,3): error C2079: 'TicTacToe<int>::_gamestate::field' uses undefined class 'std::array<std::array<TicTacToe<int>::_player,3>,3>'
1>C:\AlphaBetaNegamax\TicTacToe.h(20): message : see reference to class template instantiation 'TicTacToe<int>::_gamestate' being compiled
1>C:\AlphaBetaNegamax\Source.cpp(3): message : see reference to class template instantiation 'TicTacToe<int>' being compiled
1>C:\AlphaBetaNegamax\TicTacToe.h(24,21): error C2555: 'TicTacToe<int>::getpossiblemoves': overriding virtual function return type differs and is not covariant from 'game<evaluationtype>::getpossiblemoves'
1> with
1> [
1> evaluationtype=int
1> ]
1>C:\AlphaBetaNegamax\Negamax_and_Gamestructure.h(17): message : see declaration of 'game<evaluationtype>::getpossiblemoves'
1> with
1> [
1> evaluationtype=int
1> ]
我知道我在继承嵌套类部分的某个地方搞砸了,我不确定在哪里。那是我真的不应该做的事情,还是我对继承结构有问题?
解决方案
推荐阅读
- c++ - QNetworkConfigurationManager 的替代方案
- html - 如何在 HTML 和 CSS 中删除图像和资源管理器侧面之间的“空白”
- dns - 无法将 Google 网站发布到 Google 域网址:DNS_PROBE_FINISHED_NXDOMAIN
- spring - 从 Linux 终端启动 Spring Scheduled 作业
- c - 如何使用 fork() 知道代码中的输出?
- cmake - 使用 dockcross 从 ExternalProject_Add 构建项目
- java - Rmi UnmarshalException:错误解组返回
- c# - 为什么在 EF 核心中不能使用 OrderBy 方法?
- c++ - 如何在同一行中使用带有多个变量的 getline()?
- android - 如何使android库使用权限可选而不是必需