遗传和多态性问题
我目前正在学习 C++ 中的继承和多态性,并且在尝试创建继承并定义虚函数的父类的子类时遇到了这个问题。
目标是让 HumanPlayer
成为 Player
的子级,并创建自己的 insertion()
实现(一个插入输入的函数)进入井字游戏板,一旦完成),但是,我遇到了一些我似乎无法解决的错误。我收到一个矮人错误说
存在对“玩家的 vtable”的未定义引用。在函数 Player::Player() 和 Player::~Player() 中。
以及另一个矮人错误说
未定义对“玩家类型信息”的引用。
任何对此的帮助将不胜感激。
Player.cpp:
#include <iostream>
#include <string>
#include "Player.h"
Player::Player() {
name = "Zoe";
myState = 0;
isWon = false;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
class Player {
Player();
protected:
std::string name;
int myState;
bool isWon;
public:
virtual void insertion();
};
#endif
HumanPlayer.h :
#ifndef HUMANPLAYER_H
#define HUMANPLAYER_H
#include "Player.h"
class HumanPlayer : public Player {
public:
HumanPlayer();
void insertion();
protected:
std::string Name1;
std::string Name2;
int humanCheck=0;
std::string getName();
int toInsert;
};
#endif
HumanPlayer.cpp
#include <iostream>
#include <string>
#include "Player.h"
#include "HumanPlayer.h"
HumanPlayer::HumanPlayer() {
if(humanCheck == 0) {
std::cout<<"Player One type your name";
scanf("%s", Name1);
std::cout<<"Player Two type your name";
scanf("%s", Name2);
humanCheck++;
}
}
void HumanPlayer::insertion() {
if(humanCheck % 2 ==0) {
std::cout<<Name2<<"'s turn, please enter an integer between 1 and 7";
scanf("%i", &toInsert);
} else {
std::cout<<Name1<<"'s turn, please enter an integer between 1 and 7";
scanf("%i", &toInsert);
}
humanCheck++;
}
I'm currently learning inheritance and polymorphism in C++, and am coming across this issue when trying to create a child of a parent class that inherits and defines a virtual function.
The goal is for HumanPlayer
to be a child of Player
, and to create its own implementation of insertion()
(a function that inserts an input into a tic-tac-toe board, once complete), however, I have run into a few errors that I can't seem to get by. I'm getting a dwarf error saying
There's an undefined reference to 'vtable for Player'. In function Player::Player() and Player::~Player().
As well as another dwarf error saying
Undefined reference to 'typeinfo for Player'.
Any help with this would be greatly appreciated.
Player.cpp:
#include <iostream>
#include <string>
#include "Player.h"
Player::Player() {
name = "Zoe";
myState = 0;
isWon = false;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
class Player {
Player();
protected:
std::string name;
int myState;
bool isWon;
public:
virtual void insertion();
};
#endif
HumanPlayer.h :
#ifndef HUMANPLAYER_H
#define HUMANPLAYER_H
#include "Player.h"
class HumanPlayer : public Player {
public:
HumanPlayer();
void insertion();
protected:
std::string Name1;
std::string Name2;
int humanCheck=0;
std::string getName();
int toInsert;
};
#endif
HumanPlayer.cpp
#include <iostream>
#include <string>
#include "Player.h"
#include "HumanPlayer.h"
HumanPlayer::HumanPlayer() {
if(humanCheck == 0) {
std::cout<<"Player One type your name";
scanf("%s", Name1);
std::cout<<"Player Two type your name";
scanf("%s", Name2);
humanCheck++;
}
}
void HumanPlayer::insertion() {
if(humanCheck % 2 ==0) {
std::cout<<Name2<<"'s turn, please enter an integer between 1 and 7";
scanf("%i", &toInsert);
} else {
std::cout<<Name1<<"'s turn, please enter an integer between 1 and 7";
scanf("%i", &toInsert);
}
humanCheck++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论