I'm trying to do class inheritance in C++, but it obviously works very differently than in Python.
Right now, I have two classes, one called Player
that is the base class, and another one called HumanPlayer
that's the subclass.
The Player
class is an abstract class that has two ways of working.
The first is that it acts like a singleton. It has one static function called make_move
that people can call with an int
and a TicTacToeGame&
, and it will make a move for the player with that int
as the player's number in that game of TicTacToe
.
The second is that it works as a class for creating objects that have a player number as a property. So, if you construct an object with the class, you should get back an object with a player_number
property. Then, if you call the make_move
function with just a TicTacToeGame&
on the object, it will automatically plug in its player number and use the static class method to make the move in the game.
I want the same functionality for HumanPlayer
, except I just want to have to write a new static function for HumanPlayer
, and that's it, since the other functionality remains the same.
Here's the code:
#include <iostream>
#include <string>
using namespace std;
class TicTacToeGame {
};
class Player {
public:
static void make_move(int player_number, TicTacToeGame& game);
protected:
int player_number;
public:
explicit Player(int player_number_param) {
player_number = player_number_param;
}
public:
void make_move(TicTacToeGame& game) {
return make_move(player_number, game);
}
};
class HumanPlayer: public Player {
public:
static void make_move(int player_number, TicTacToeGame& game) {}
public:
HumanPlayer(int player_number_param): Player(player_number_param) {}
};
int main()
{
TicTacToeGame game;
HumanPlayer human_player = HumanPlayer(2);
human_player.make_move(game);
return 0;
}
I learned recently that subclasses don't inherit constructors, so it turns out I have to write both a new static function and a constructor, which I have done.
However, whenever I initialize a new HumanPlayer
object, the compiler can't seem to find a match for the make_move(TicTacToeGame&)
method, and I'm not sure why.
The specific error message I'm getting is
C:\Users\London\Desktop\Python Programs\LearningC++\FirstProgram_SO.cpp: In function 'int main()': C:\Users\London\Desktop\Python Programs\LearningC++\FirstProgram_SO.cpp:41:29: error: no matching function for call to 'HumanPlayer::make_move(TicTacToeGame&)' human_player.make_move(game); ^ C:\Users\London\Desktop\Python Programs\LearningC++\FirstProgram_SO.cpp:29:15: note: candidate: static void HumanPlayer::make_move(int, TicTacToeGame&) static void make_move(int player_number, TicTacToeGame& game) {} ^~~~~ C:\Users\London\Desktop\Python Programs\LearningC++\FirstProgram_SO.cpp:29:15: note: candidate expects 2 arguments, 1 provided
How can I get the HumanPlayer
class to work in the same way the Player
class does?
The redefinition of the static function with the same name is hiding the one you want to use.
Either rename it differently or add
public:
using Player::make_move;
Note that unlike Java you don't need to repeat public:
before every function, the same visibility applies as long as you don't change it.
class YourClass {
public:
void foo1(); // public
void bar1(); // also public
protected:
void foo2(); // protected
void bar2(); // also protected
};