In my TicTacToe game I having some trouble with virtual functions. The following code throws an error in Dev C++: "class std::auto_ptr' has no member named 'makeAMove'.
According to the error, the problem has something to do with the makeAMove function, but I can't see what's wrong. Also, I should mention that I'm using the deprecated auto_ptr function instead of unique_ptr because apparently the teacher's aid grading my code does not have a C++ 11 compliant compiler.
Right now both playGame and makeAMove functions don't do anything, but I want to figure out what's causing the error before I proceed.
Thank you for any suggestions.
Here's the relevant code:
Game.h (acts as controller for the game)
#include "Player.h"
#include "AIPlayer.h"
#include "HumanPlayer.h"
#include <memory>
class Game
{
public:
Game(unsigned int players)
{
if (players == 1)
{
player1.reset (new HumanPlayer());
player2.reset (new AIPlayer());
}
else
{
player1.reset (new HumanPlayer());
player2.reset (new HumanPlayer());
}
}
void playGame()
{
player1.makeAMove();
}
private:
std::auto_ptr<Player> player1; // pointer to a Player object (C++11 has more secure unique_ptr)
std::auto_ptr<Player> player2; // pointer to a Player object
};
Player.h (base class for both HumanPlayer.h and AIPlayer.h)
class Player
{
public:
virtual void makeAMove() = 0; // will accept pointer to board object as param
};
HumanPlayer.h
class HumanPlayer : public Player
{
public:
virtual void makeAMove()
{
// do some work
}
};
AIPlayer.h
#pragma once // include guard
#include "Player.h"
class AIPlayer : public Player
{
public:
virtual void makeAMove()
{
// do some work
}
};
main.cpp
#include <iostream>
#include "Game.h"
int main()
{
Game myGame(1);
myGame.playGame();
return 0;
}
I think your intention is to do:
You need to call
makeAMove
on the player object owned by the auto_ptr, not on the auto_ptr itself. Although you can dereference the auto_ptr just like you would a normal pointer (as the others have pointed out) thanks to overloaded operators.