【问题标题】:C++ implementing abstract classesC++ 实现抽象类
【发布时间】:2018-11-25 05:22:22
【问题描述】:

我正在尝试编写一个名为 TerminalPlayer 的派生类,它继承了一个 Player 类,并声明了 virtual const Card playCard(const Card contrastCard) = 0;你将如何在抽象类中实现继承的 playCard 以及原型末尾的 = 0 是什么意思?

我在主测试代码中也出现了错误:无法分配抽象类型“播放器”的对象。我认为这是因为我没有正确实现 Player 类,但我不知道如何修复它。

播放器.h

    #ifndef PLAYER_H_
    #define PLAYER_H_

    #include <vector>
    #include "Card.h"

    #define MAX_HAND_SIZE 3

    // Abstract Player classS
    class Player {

        public:
            // Deconstructor
            virtual ~Player() {
            }

            // Play a card. If the player receives a joker then this player is going first
            virtual const Card playCard(const Card opponentCard) = 0;

            // Receive a card from the dealer
            void receiveCard(const Card c) {
                hand.push_back(c);
            }

            // Add points to the score
            void addScore(unsigned s) {
                score += s;
            }

            // Get the score
            int getScore() const {
                return score;
            }

            // Return true if the player has cards in the hand
            bool hasCards() const {
                return (hand.size() != 0);
            }

            // Receive the cards played from the previous round. This member function would be used by a computer player that may need to 'see' what cards were played.
            void cardsPlayed(const Card card1, const Card card2) {

            }

            // Output the players name
            friend std::ostream& operator <<(std::ostream& out, const Player& p);

        protected:
            // Constructor. Since this is an abstract class we do not want anyone instantiating a player class so we make it protected.
            Player(std::string name) :
                    score(0), name(name), hand(0) {
            }

            int score;
            std::string name;
            std::vector<Card> hand;
    };

    #endif

TerminalPlayer.h

    #ifndef TERMINALPLAYER_H_
    #define TERMINALPLAYER_H_

    #include "Player.h"

    class TerminalPlayer : public Player {
    public:
        TerminalPlayer(std::string name);
        virtual ~TerminalPlayer();
    };

    #endif

终端播放器.cpp

    #include "Player.h"
    Card playCard(const Card opponnentCard){
        // TODO: playCard code here
    }

Test.cpp

    int main(){

        // This initialization give error: cannot allocate an object of abstract type ‘Player’
        TerminalPlayer player1 = Player("Player1");

        return 0;
    }

【问题讨论】:

  • 在您的问题中粘贴完整的错误消息。
  • 错误:Test.cpp:13:43:错误:无法分配抽象类型“Player”的对象错误:est.cpp:13:17:错误:无法将变量“player1”声明为抽象类型'TerminalPlayer' Eclipse 给出错误,错误:类型'TerminalPlayer'必须实现继承的纯虚拟方法'Player::playCard'错误:类型'Player'必须实现继承的纯虚拟方法'Player::玩牌'

标签: c++ inheritance abstract-base-class


【解决方案1】:

= 0' 表示这是一个pure virtual 函数。 这种类型的函数必须由从基类继承并在程序中实例化的任何类定义。

由于您的基类声明:

// Play a card. If the player receives a joker then this player is going first
virtual const Card playCard(const Card opponentCard) = 0;

您应该在派生类中实现此功能。 你在 TerminalPlayer.cpp 中接近了:

const Card TerminalPlayer::playCard(const Card opponnentCard){
    // TODO: playCard code here
}

您缺少上面显示的TerminalPlayer:: 范围。 还缺少派生类中的函数声明。您需要添加:

virtual const Card playCard(const Card opponentCard) override;

TerminalPlayer 类中。把它放在析构函数之后。

应该可以的。

一个想法:返回值上的 const 限定符不是必需的,因为您是按值返回。

【讨论】:

  • 这些小改动让这些错误消失了,但我仍然需要创建一个 TerminalPlayer 类型的变量并调用 Protected Player 构造函数基类。我无法弄清楚如何声明 Test.cpp 部分的 OP 中列出的 TerminalPlayer 类型的变量:TerminalPlayer player1 = Player("player1");
  • 终端播放器 player1 = Player("player1");完全按照您所说的去做。它将 TerminalPlayer 实例化为 player1,它应该调用受保护的 ctor。
  • 我知道发生了什么。给出的错误不正确,但最终导致派生类中没有析构函数(即使它是空白的)。一旦我添加了一个空白析构函数,它就会编译。顺便说一句,感谢您的原始回答,解决了我遇到的最大问题。
猜你喜欢
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 2014-11-02
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多