使用虚拟功能在C 中进行猜测

Using virtual functions to make a guessing game in C++

本文关键字:虚拟 功能      更新时间:2023-10-16

我正在为我的C 类做我的最终作业之一,我感到困惑。

说明:

下面列出的是玩一个猜测游戏的代码,其中两个玩家试图猜测一个数字。您的任务是用代表人类玩家或计算机玩家的对象扩展程序。

播放功能作为输入两个播放器对象。使用名为getGuess((的虚拟函数定义播放器类。player :: getGuess((的实现可以简单地返回0。接下来,定义一个名为"人类玩家"的类。HumanPlayer :: GetGuess((的实现应提示用户输入一个数字并返回从键盘输入的值。接下来,定义一个名为"计算机玩家"的类。Computer Player :: GetGuess((的实现应随机选择一个从0到100的数字。最后,构建一个主要功能,该功能通过两个人类玩家的实例来调用Play(Player& Player1,Player& Player2((人类与人类与人类的实例(,一个人类游戏和计算机游戏的实例(人与计算机(,以及两个计算机播放器的实例(计算机与计算机(。

#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Player {
   public:
      virtual int getGuess();
      int guess;
};
class ComputerPlayer : public Player {
   public:
      ComputerPlayer() : Player(){}
      int getGuess() {
         return rand() % 100 + 1;
      }
};
class HumanPlayer : public Player {
   public:
      HumanPlayer() : Player() {}
      int getGuess() {
         cout << "Please enter your guess: ";
         cin >> guess;
         return guess;
      }
};

bool checkForWin(int guess, int answer)
{
  if (answer == guess)
  {
     cout << "You're right! You win!" << endl;
     return true;
  }
  else if (answer < guess)
     cout << "Your guess is too high." << endl;
  else
     cout << "Your guess is too low." << endl;
  return false;
}
void play(Player &player1, Player &player2)
{
  int answer = 0, guess = 0;
  answer = rand() % 100;
  bool win = false;
  while (!win)
  {
   cout << "Player 1's turn to guess." << endl;
   guess = player1.getGuess();
   win = checkForWin(guess, answer);
   if (win) return;
   cout << "Player 2's turn to guess." << endl;
   guess = player2.getGuess();
   win = checkForWin(guess, answer);
  }
}
int main()
{
    srand(1);
    HumanPlayer humanPlayer1, humanPlayer2;
    ComputerPlayer computerPlayer1, computerPlayer2;
    cout << "Human vs Humann";
    cout << "Press a key to continue...";
    char c;
    cin >> c;
    play(humanPlayer1, humanPlayer2);
    cout << "Human vs Computern";
    cout << "Press a key to continue...";
    cin >> c;
    play(humanPlayer1, computerPlayer1);
    cout << "Computer vs Computern";
    cout << "Press a key to continue...";
    cin >> c;
    play(computerPlayer1, computerPlayer2);
    return 0;
}

我不断收到以下编译错误:

/tmp/cctJstdH.o: In function `Player::Player()':
main.cpp:(.text._ZN6PlayerC2Ev[_ZN6PlayerC5Ev]+0x9): undefined reference to `vtable for Player'
/tmp/cctJstdH.o:(.rodata._ZTI11HumanPlayer[_ZTI11HumanPlayer]+0x10): undefined reference to `typeinfo for Player'
/tmp/cctJstdH.o:(.rodata._ZTI14ComputerPlayer[_ZTI14ComputerPlayer]+0x10): undefined reference to `typeinfo for Player'
collect2: error: ld returned 1 exit status

Player::getGuess()虚拟方法没有实现。添加= 0

,添加实现或将类声明为纯虚拟
class Player
{
    // ...
    virtual int getGuess() = 0;
};

超级阅读指令。

" player :: getGuess((的实现可以简单地返回0。"

您的class Player根本无法实现Player::getGuess()

这应该解决:

class Player {
   public:
      virtual int getGuess() { return 0; } // implement this function
      int guess;
};