How to access the object pointed to by a std::auto_ptr

402 views Asked by At

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;
}
3

There are 3 answers

1
AudioBubble On

I think your intention is to do:

player1.get()->makeAMove();

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.

0
Elliot Robinson On

Bear in mind that an auto_ptr acts just like a pointer, so you have to dereference (either with operator* or operator->).

2
Zac Howland On

The smart pointer wrappers behave just like pointers. That is, when you want to use the pointer functions (in this case makeAMove), you need to dereference it:

player1->makeAMove();

Basically what happens here is the operator-> will return a Player* allowing you to call the appropriate member function of Player. using the operator. will give you access to the auto_ptr member functions (which is not what you want at that point in your code).