使用虚拟功能在C 中进行猜测
Using virtual functions to make a guessing game in C++
我正在为我的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;
};
相关文章:
- 如何在C++中获得"静态纯虚拟"功能?
- std::is_trivially_copyable_v 关于虚拟功能
- 隐藏重载虚拟功能的模板化访客:SFINAE 在使用?
- 为什么我无法覆盖虚拟功能?
- 虚拟功能不起作用
- 如果在创建对象时创建了 VPTR,那么为什么具有虚拟功能的类的大小在 32 位系统上为 4,在 64 位机器上为 8
- 虚拟功能如何影响static_cast?
- __declspec(属性)和虚拟功能
- 实现来自第三方类的虚拟功能
- 消除未使用的虚拟功能
- 为什么虚拟继承即使不涉及虚拟功能也需要 vtable?
- C 虚拟功能的输出
- 抽象类 A 由类 C 继承"through"类 B "intermediary"。如何声明虚拟功能?
- 虚拟功能是否不太可能导致堆栈溢出
- 使用继承和虚拟功能应用五个规则
- 两个派生类(十六进制 二进制)的虚拟功能
- 虚拟功能编译器优化C
- 虚拟功能指针指向派生类中的函数
- 隐藏的过载虚拟功能OSX QT4
- 使用虚拟功能在C 中进行猜测