我正在用C ++编写国际象棋游戏, Player 具有16个件的数组,这是每个单独件的抽象类(马,典当,国王等)。编译器给我一个“ pecas”无效的抽象类型“ peca”。我在做什么错?谢谢!
Player.h
#include "Peca.h" // Includes Piece abstract class
using std::string;
class Jogador
{
private:
static int numeroDeJogador; //PlayerNumber (0-1)
string nome;
Peca pecas[16]; //This is the array of the abstract class Pecas (Pieces), where i want to put derived objects like Horse, king..
public:
string getNomeJogador(); // Return the player name
};
Pieces.h
#ifndef PECA_H
#define PECA_H
#include <string>
using std::string;
class Peca {
private:
int cor; //0 para as brancas, 1 para as pretas
bool emJogo;
public:
Peca(int cor);
virtual string desenha() = 0;
virtual bool checaMovimento(int linhaOrigem, int colunaOrigem, int linhaDestino, int colunaDestino) = 0;
int getCor();
bool estaEmJogo();
void setForaDeJogo(bool estado);
};
#endif
派生类示例:
#include "Peca.h"
using std::string;
class Cavalo : public Peca {
public:
Cavalo(int cor);
bool checaMovimento(int linhaOrigem, int colunaOrigem, int linhaDestino, int colunaDestino);
string desenha();
};
答案 0 :(得分:6)
一个数组要求该数组的对象是可构造的。您无法构造Peca
,因此无法包含它们的数组。
您需要的是指向Peca
的指针的容器。指针始终是可构造的,即使它们不能指向。在这种情况下,您可以使用std::array<std::unique_ptr<Peca>, 16> pecas
,以便有一个托管指针数组。