在派生类中实现虚方法时出现问题

发布于 2024-12-05 20:00:06 字数 813 浏览 1 评论 0原文

嘿,我试图使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-12-12 20:00:06

首先,您需要在 Shapes 类中再次声明绘制函数。然后确保它与 Renderable 类中声明的签名具有相同的签名。

//Shapes.h
 #ifndef H_SHAPES_
 #define H_SHAPES_
 #include "Renderable.h"
 #include "Entity.h"
 class Shapes : public Entity, public Renderable
 {
 public:
     Shapes();
     ~Shapes();

     virtual bool Draw();

 };


 #endif

 //shapes.cpp

bool Shapes::Draw()
{
}

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.

//Shapes.h
 #ifndef H_SHAPES_
 #define H_SHAPES_
 #include "Renderable.h"
 #include "Entity.h"
 class Shapes : public Entity, public Renderable
 {
 public:
     Shapes();
     ~Shapes();

     virtual bool Draw();

 };


 #endif

 //shapes.cpp

bool Shapes::Draw()
{
}
讽刺将军 2024-12-12 20:00:06

您的返回类型不匹配。 Renderable::Draw 返回 bool,而 Shapes::Draw 返回 void。整个函数签名(包括返回类型)必须匹配,否则派生类中的函数只是隐藏了基类中的函数。

Your return types are not matching. The Renderable::Draw returns bool, and you Shapes::Draw returns void. 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.

残疾 2024-12-12 20:00:06

您需要在 Shapes 中声明 Draw:

class Shapes : public Entity, public Renderable
{
  public:
    Shapes();
    ~Shapes();
    virtual void Draw(); 
};

仅稍后定义它是不够的。

You need to declare Draw in Shapes:

class Shapes : public Entity, public Renderable
{
  public:
    Shapes();
    ~Shapes();
    virtual void Draw(); 
};

Just defining it later is not enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文