c++ - Qt - 从基类继承类

标签 c++ qt inheritance

我正在用 Qt 制作贪吃蛇游戏,到目前为止,我还没有成功地从另一个类继承我的一个类。我可以让我的类继承自 Qt 类,如 QObjectQGraphicsRectItem,但不能继承我自己的类。

这是此问题的示例及其错误消息:

#ifndef SNAKE_H
#define SNAKE_H

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QWidget>
#include <QGraphicsRectItem>
#include <QObject>
#include <QTimer>
#include <QKeyEvent>

class Head;
class Food;
class Base; //Error 1

class Snake: public QGraphicsView, public Base //Error 2
{
    Q_OBJECT

protected:
    const static int width = 820;
    const static int height = 500;

public:
    Snake();
    QGraphicsScene * scene;
    QGraphicsView * view;
    QGraphicsRectItem * border;
    QGraphicsRectItem * border2;

};

#endif // SNAKE_H

////// Errors /////////
/*

Error 1:
  forward declaration of 'class Base'
   class Base;
         ^

Error 2: 
  invalid use of incomplete type 'class Base'
   class Snake: public QGraphicsView, public Base
                                             ^

*/

那么我到底做错了什么?为什么它不能正确继承,为什么它不能让我对 class Base 进行前向声明?

谢谢!

最佳答案

您需要包含包含 Base 类的头文件,而不是 class Base(即 #include "Base.h")

关于c++ - Qt - 从基类继承类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33728369/

相关文章:

c++ - QT Creator - QStackedWidget 页面问题

c++ - 用户定义的构造函数重载与参数父类(super class)的重载参数不匹配

swift - Swift 中的继承有点令人困惑

python - Python 中的可索引弱有序集

c++ - 在 vector C++ 上操作时出现运行时断言错误

c++ - 如何使用cmake正确链接库?

c++ - 使用数组作为映射键 : impossible even with custom allocator?

C++(14)——googletest未定义标识符

c++ - Qt 布局间隔大小

c++ - 继承 QCalendarWidget 时 paintCell() 函数中的 Painter 错误