我有一个名为Player的抽象基类,它有一个名为QuarterBack的子类。如果我尝试实例化QuarterBack的对象,则会收到2条错误,指出:a)不允许抽象类类型为“ QuarterBack”的对象:b)'QuarterBack':无法实例化抽象类
Player中有两个纯虚拟函数,分别称为getPlayerPosition()和play()。但是,我只对getPlayerPosition()报错,应该将Quarter back作为Player的具体子类,但是编译器说QuarterQuar仍然是一个抽象类。
我不知道从哪里开始解决这些错误。
class Player {
private:
string fname, lname, position;
int playerID;
static int numPlayers;
public:
Player(string fname, string lname, string position, int playerID) {
this->fname = fname;
this->lname = lname;
this->position = position;
this->playerID = playerID;
++numPlayers;
cout << "Creating Player" << endl;
}
~Player() {
cout << "Destroying Player" << endl;
}
string getName() {
return fname + " " + lname;
}
virtual string getPlayerPosistion() = 0; // pure virtual function
virtual int play() = 0; // pure virtual function
string toString() {
string temp = "Name: " + fname + " " + lname + " | Position: " + position + " | Number: " + to_string(playerID);
return temp;
}
};
class QuarterBack : public Player {
private:
string fname, lname, position;
int playerID;
public:
QuarterBack(string fname, string lname, string position, int playerID) : Player(fname, lname, position, playerID) {
this->fname = fname;
this->lname = lname;
this->position = position;
this->playerID = playerID;
cout << "Creating QuarterBack " << endl;
}
~QuarterBack() {
cout << "Destroying QuarterBack" << endl;
}
string getName() {
return fname + " " + lname;
}
string getPlayerPosition() {
return position;
}
int play() {
return 0;
}
};
int main()
{
string firstN, lastN, position;
int ID;
firstN = "Tom";
lastN = "Johnson";
position = "QuarterBack";
ID = 17;
QuarterBack qb(firstN, lastN, position, ID);
cout << qb.toString() << endl;
system("pause");
return 0;
}