Reputation: 1661
I'm making a snake game with Qt, and so far I have not once been successful in inheriting one of my classes from another one of my classes. I can get my classes to inherit from Qt classes like QObject
or QGraphicsRectItem
, but not from my own.
Here is an example of this problem, along with its error message:
#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
^
*/
So what exactly am I doing wrong here? Why won't it inherit properly and why won't it let me make a forward declaration of class Base
?
Thanks!
Upvotes: 0
Views: 572
Reputation: 49289
invalid use of incomplete type 'class Base' class Snake: public QGraphicsView, public Base
Compiler errors might often be obscure, but this one isn't. You can't use a forward declared class, unless for pointers to it. That includes inheriting it as well. You need to have the class you are inheriting beforehand, either by having it in the source or including it.
Upvotes: 0
Reputation: 79
Instead of class Base
you need to include the header file containing the Base
class (i.e. #include "Base.h"
)
Upvotes: 2