对于我的井字游戏,我有一个基本类Player和两个派生类HumanPlayer和AIPlayer。当我第一次创建类时,我将HumanPlayer类全部写在一个.h文件中。现在,由于我分离了实现文件和头文件,因此在Dev C ++中出现错误:“'{'标记之前的预期类名”。

我似乎找不到问题。

这是HumanPlayer类和Player的基类。也许有人可以发现问题。
播放器

#pragma once // include guard
#include "Board.h"

class Player
{
    public:
        virtual void makeAMove(Board &myBoard) = 0; // will accept pointer to board object as param
};


人类播放器

#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();
};


人类播放器

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

最佳答案

我看不到标头HumanPlayer.h包含类Player的定义。

08-17 04:56