对于我的tic tac toe游戏,我有一个基类Player和两个派生类HumanPlayer和AIPlayer。当我第一次创建类时,我在一个.h文件中编写了HumanPlayer类。现在,由于我已经分离了实现和头文件,我在Dev C ++中遇到了一个错误:“'{'token'之前的预期类名。
我似乎无法找到问题。
这是HumanPlayer类和基类Player。也许有人可以发现问题。 Player.h
#pragma once // include guard
#include "Board.h"
class Player
{
public:
virtual void makeAMove(Board &myBoard) = 0; // will accept pointer to board object as param
};
HumanPlayer.h
#pragma once // include guard
#include "Board.h"
class HumanPlayer: public Player
{
public:
HumanPlayer(char token);
virtual void makeAMove(Board &myBoard);
private:
char token;
int askForRow();
int askForColumn();
};
HumanPlayer.cpp
#include "HumanPlayer.h"
#include <cstdlib> // atoi
HumanPlayer::HumanPlayer(char token)
{
this->token = token;
}
void HumanPlayer::makeAMove(Board &myBoard)
{
bool done = false;
do
{
int row = askForRow();
int column = askForColumn();
if (myBoard.getCell(row, column) == ' ')
{
myBoard.setCell(row, column, token);
done = true;
}
else
std::cout << "This cell is already occupied. Try a different cell" << std::endl;
}
while (!done);
}
int HumanPlayer::askForRow()
{
while(true)
{
std::cout << "Enter a row (0, 1, 2) for player " << token << ": ";
std::string row_s;
std::getline(std::cin, row_s);
if (row_s == "0" || row_s == "1" || row_s == "2")
{
return atoi(row_s.c_str());
}
else
{
std::cout << "\nInvalid input\n";
}
}
}
int HumanPlayer::askForColumn()
{
while(true)
{
std::cout << "Enter a column (0, 1, 2) for player " << token << ": ";
std::string column_s;
std::getline(std::cin, column_s);
if (column_s == "0" || column_s == "1" || column_s == "2")
{
return atoi(column_s.c_str());
}
else
{
std::cout << "\nInvalid input\n";
}
}
}
答案 0 :(得分:2)
我没有看到标题HumanPlayer.h包含类Player的定义。