如何访问std :: auto_ptr指向的对象

nav*_*8tr 0 c++ auto-ptr

在我的TicTacToe游戏中,我遇到了虚拟功能的问题.以下代码在Dev C++中引发错误:"class std :: auto_ptr"没有名为'makeAMove'的成员.

根据错误,问题与makeAMove函数有关,但我看不出有什么问题.另外,我应该提一下,我正在使用已弃用的auto_ptr函数而不是unique_ptr,因为显然教师对我的代码进行分级的辅助工具没有符合C++ 11的编译器.

现在playGame和makeAMove函数都没有做任何事情,但我想在继续之前找出导致错误的原因.

谢谢你的任何建议.

这是相关的代码:

Game.h(充当游戏的控制器)

#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
};
Run Code Online (Sandbox Code Playgroud)

Player.h(HumanPlayer.h和AIPlayer.h的基类)

class Player
{
    public:
        virtual void makeAMove() = 0; // will accept pointer to board object as param
};
Run Code Online (Sandbox Code Playgroud)

HumanPlayer.h

class HumanPlayer : public Player
{
    public:
        virtual void makeAMove()
        {
            // do some work
        }
};
Run Code Online (Sandbox Code Playgroud)

AIPlayer.h

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

class AIPlayer : public Player
{
    public:
        virtual void makeAMove()
        {
            // do some work
        }
};
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <iostream>
#include "Game.h"

int main() 
{
    Game myGame(1);
    myGame.playGame();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ell*_*son 5

请记住,auto_ptr行为就像一个指针,因此您必须取消引用(使用operator*或operator->).