navig8tr
navig8tr

Reputation: 1844

C++ Why am I getting an error with this derived class

For my tic tac toe game I have a base class Player and two derived classes HumanPlayer and AIPlayer. When I first made the classes I wrote the HumanPlayer class all in one .h file. Now, since I have separated the implementation and header files, I'm getting an error in Dev C++: "expected class-name before '{' token".

I can't seem to find the problem.

Here is the HumanPlayer class and the base class Player. Maybe someone can spot the problem. 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";
        }
    }
}

Upvotes: 0

Views: 104

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

I do not see where header HumanPlayer.h includes definition of class Player.

Upvotes: 2

Related Questions