在派生类中实现虚方法时出现问题
嘿,我试图使用 MVS2010 编译器多重继承纯虚函数。所以我可以对所有可渲染对象进行绘制。
图表
这是ASCII 的
|Renderable | |Entity |
|virtual bool draw()=0;| | functions in here |
is - a is - a
Shape
所以看起来它不会让我继承纯虚函数?并实现虚函数。这是我的代码。
// Renderable.h
#ifndef H_RENDERABLE_
#define H_RENDERABLE_
class Renderable
{
public:
virtual bool Draw() = 0;
};
#endif
//Shapes.h
#ifndef H_SHAPES_
#define H_SHAPES_
#include "Renderable.h"
#include "Entity.h"
class Shapes : public Entity, public Renderable
{
public:
Shapes();
~Shapes();
};
#endif
//shapes.cpp
#include "Shapes.h"
Shapes::Shapes()
{
}
Shapes::~Shapes()
{
}
virtual void Shapes::Draw()
{
}
我已经尝试了多种方法,但谷歌搜索也不起作用。
Hey I was trying to multiple inherit a pure virtual function using MVS2010 Compiler. So I can run a draw for all renderable objects.
So here is the diagram
in ASCII
|Renderable | |Entity |
|virtual bool draw()=0;| | functions in here |
is - a is - a
Shape
So it seems it wont let me inherit the pure virtual function? and implement the virtual function. Here is my code.
// Renderable.h
#ifndef H_RENDERABLE_
#define H_RENDERABLE_
class Renderable
{
public:
virtual bool Draw() = 0;
};
#endif
//Shapes.h
#ifndef H_SHAPES_
#define H_SHAPES_
#include "Renderable.h"
#include "Entity.h"
class Shapes : public Entity, public Renderable
{
public:
Shapes();
~Shapes();
};
#endif
//shapes.cpp
#include "Shapes.h"
Shapes::Shapes()
{
}
Shapes::~Shapes()
{
}
virtual void Shapes::Draw()
{
}
I have tried multiple things and it doesn't work also google searched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您需要在 Shapes 类中再次声明绘制函数。然后确保它与 Renderable 类中声明的签名具有相同的签名。
First, you need to declare the draw function again in your Shapes class. Then make sure it has the same signature as the one declared in the Renderable class.
您的返回类型不匹配。
Renderable::Draw
返回bool
,而Shapes::Draw
返回void
。整个函数签名(包括返回类型)必须匹配,否则派生类中的函数只是隐藏了基类中的函数。Your return types are not matching. The
Renderable::Draw
returnsbool
, and youShapes::Draw
returnsvoid
. The entire function signature (including the return types) must match, otherwise the function in the derived class simply hides the function in the base class.您需要在 Shapes 中声明 Draw:
仅稍后定义它是不够的。
You need to declare Draw in Shapes:
Just defining it later is not enough.