Visual c++ visualc&x2B+;-无法调用派生类函数
//在我正在制作的垄断游戏中,我有一个叫做Square的基类。由此派生的一个类称为freeParking。freeParking类有一个名为playSquare的函数,我正在尝试调用这个函数,但是main中的playSquare函数调用给了我错误。以下是我尝试使用的代码: //基方类Visual c++ visualc&x2B+;-无法调用派生类函数,visual-c++,inheritance,polymorphism,derived-class,Visual C++,Inheritance,Polymorphism,Derived Class,//在我正在制作的垄断游戏中,我有一个叫做Square的基类。由此派生的一个类称为freeParking。freeParking类有一个名为playSquare的函数,我正在尝试调用这个函数,但是main中的playSquare函数调用给了我错误。以下是我尝试使用的代码: //基方类 #pragma once #include <string> #include <iostream> using namespace std; class Square { publ
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Square
{
public:
Square(string d);
string displayName();
private:
string squareName;
};
#pragma once
#include "Square.h"
#include "Player.h"
#include <iostream>
class freeParking : public Square
{
public:
freeParking(string a);
void playSquare(Player *player, Player *otherPlayer);
private:
};
#pragma一次
#包括
#包括
使用名称空间std;
阶级广场
{
公众:
正方形(弦d);
字符串displayName();
私人:
字符串名称;
};
//从Square类继承的freeParking类
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Square
{
public:
Square(string d);
string displayName();
private:
string squareName;
};
#pragma once
#include "Square.h"
#include "Player.h"
#include <iostream>
class freeParking : public Square
{
public:
freeParking(string a);
void playSquare(Player *player, Player *otherPlayer);
private:
};
#pragma一次
#包括“Square.h”
#包括“Player.h”
#包括
等级免费停车场:公共广场
{
公众:
免费停车(a列);
void playSquare(玩家*玩家,玩家*其他玩家);
私人:
};
//免费停车playSquare功能
#include "FreeParking.h"
using namespace std;
freeParking::freeParking(string a)
:Square(a)
{
}
void freeParking::playSquare(Player *player, Player *otherPlayer)
{
cout << "Player " << player->returnPlayerPiece() << " lands on free parking" << endl;
cout << "Player " << player->returnPlayerPiece() << " is resting" << endl;
}
#包括“FreeParking.h”
使用名称空间std;
freeParking::freeParking(字符串a)
:正方形(a)
{
}
无效免费停车::playSquare(玩家*玩家,玩家*其他玩家)
{
你可能还没有把playSquare
作为Square
界面的一部分。你似乎希望playSquare
特定于Square的类型,但你也希望能够在Square*
上调用它,而不知道Square
的类型。要做到这一点,请将playSquare
作为一种虚拟方法d
class Square {
string squareName;
public:
Square(string d);
string displayName();
virtual void playSquare(Player* p1, Player* p2) = 0;
...
};
将virtual
添加到派生类中playSquare
声明的前面。这是一个纯虚拟函数,这意味着它不在基类中定义,但必须在实际实例化的任何派生类中定义。如果不喜欢此限制,请将=0
替换为{}
为其提供一个空的默认定义。我猜,在给定游戏板上下文的情况下,您需要为每个派生类提供一个定义
编辑
不能使用std::vector
存储多态对象(具有虚拟函数的对象)。在这种情况下,向量将只为其模板参数的类型分配内存。Square
。与派生类相关的任何数据都将被切断。您可以使用std::vector
来解决此问题。由于对象不再存储在向量中,因此我们不再存在此问题
另一方面,如果不知道Square
是什么类型的Square
对象,则无法复制Square
对象,因为Square
复制构造函数不知道如何处理对象的freeParking
部分
我还注意到,您使用两个循环计数器并没有什么特别的原因。请去掉a
,因为您已经定义的迭代器允许您访问向量的每个元素
for(vector<Square*>::iterator it = squaresVector.begin(); it != squaresVector.end(); ++it) {
(*it)->playSquare(p1, p2); //p1 and p2 are of type Player*
}
for(vector::iterator it=squaresVector.begin();it!=squaresVector.end();++it){
(*it)->playSquare(p1,p2);//p1和p2属于Player类型*
}
我怀疑您在传递playSquare
的参数时是否打算声明这些参数。您好,谢谢您的帮助,它现在可以工作了。但是,如果我将这些正方形存储在数组中,它会为正确的派生类调用相应的函数。但是,当从我最初的帖子中定义的向量调用它时,它会调用基函数类playSquare函数(我永远不想调用它)有什么进一步的建议吗?我编辑了我的答案,将其和我在仔细检查时发现的一些其他错误包括在内。