没有调整函数调用class :: class
我是新来的c + +和正在pacman原型上工作。在试图把握多态的概念的同时,我遇到了这个问题。 每次我尝试为Ghost和ScaredGhost类创建构造函数或对象时,最终都会使用No matching函数调用class :: class错误。没有调整函数调用class :: class
继承人我的头文件:
class Enemies : public Window
{
public:
Enemies(const Window &window,int w,int h,int x,int y);
virtual ~Enemies();
virtual void EnemyDraw() = 0;
protected:
int gh,gw; //Height width
int gx,gy; // x and y
};
//--------------------------------------------------------
class Ghost : public Enemies
{
public:
Ghost();
void EnemyDraw();
};
//--------------------------------------------------------
class ScaredGhosts : public Enemies
{
public:
void EnemyDraw();
};
和实现:
Enemies::Enemies(const Window &window,int w,int h,int x,int y):
Window(window), gw(w), gh(h), gx(x),gy(y)
{
//ctor
}
Enemies::~Enemies(){}
//_________________________________________________________________________________________________________
void Ghost::EnemyDraw()
{
SDL_Rect rect;
rect.w = gw;
rect.h = gh;
rect.x = gx;
rect.y = gy;
SDL_SetRenderDrawColor(renderer,0,0,255,0);
SDL_RenderFillRect(renderer,&rect);
}
Ghost::Ghost()
{
}
//_________________________________________________________________________________________________________
void ScaredGhosts::EnemyDraw()
{
SDL_Rect rect;
rect.w = gw;
rect.h = gh;
rect.x = gx;
rect.y = gy;
SDL_SetRenderDrawColor(renderer,0,255,255,0);
SDL_RenderFillRect(renderer,&rect);
}
这是这个类的只是基础,但我不能没有解决这个问题继续进行。
Ghost::Ghost()
{
}
此构造隐含试图调用所有的基本类型的无参数的构造函数。但是,没有无参数构造函数Enemies
,其中Ghost
继承,所以此调用失败。 (这是“没有匹配呼叫Enemies :: Enemies()”的地方 - 编译器告诉你Ghost::Ghost()
试图调用这个构造函数,但它不存在。)
单向解决这一问题将是接受该Ghost
构造相同的参数,并进行转发:
class Ghost : public Enemies {
public:
Ghost(const Window &window,int w,int h,int x,int y);
// ...
};
Ghost::Ghost(const Window &window,int w,int h,int x,int y)
: Enemies(window, w, h, x, y)
{
}
你需要做同样的事情ScaredGhost
。
C++ 11允许你继承这样的构造,这基本上是比较容易,远不易出错:
class Ghost : public Enemies {
public:
using Enemies::Enemies;
// ...
};
非常感谢!它帮助 –
@JuliusTumas如果有帮助,考虑接受答案 –
这是ABD一般类的名字不应该是plural.s很难看到'Enemies'可以是一个'Window.'另外,我建议你在不使用继承的情况下编写你的游戏 - 继承被初学者大量使用。 –