1 year ago
#374715
StrugglingCSStudent
Issues with inheritance and polymorphism
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++;
}
c++
oop
inheritance
polymorphism
virtual-functions
0 Answers
Your Answer