[SOLVED]Need Help: Error " 'CLASS' does not name a type"
-
I have default mainwindow class: mainwindow.h and mainwindow.cpp. I also have my own class Sprite: sprite.h and sprite.cpp. I want to create the object of Sprite class in mainwindow.h but I've got an error : "'Sprite' does not name a type'". I included header file "sprite.h" in mainwindow.h but problem is still there!
Here is my mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <SOME QT HEADERS> #include "sprite.h" namespace Game{ const double version = 0.01; const int FPS = 60; const int WIDTH = 800; const int HEIGHT = 600; } class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); private: QColor mainGameColor; Sprite mainSprite; void update(); void paintEvent(QPaintEvent *event); };
...and sprite.h
#ifndef SPRITE_H #define SPRITE_H #include <QObject> #include "mainwindow.h" using namespace std; class Sprite : public QObject { Q_OBJECT public: Sprite(QObject *parent = 0,string file = "default.bmp"); QImage getSprite(); signals: public slots: private: QImage image; int x; int y; }; #endif // SPRITE_H
I will be grateful for any advices!
-
You should remove inclusion of mainwindow.h from sprite.h: you have introduced a circular dependency:
- The compiler reads mainwindow and encounters sprite.h include
- It proceeds to sprite.h, but finds mainwindow.h include there
- It goes back to mainwindow.h, as instructed
- And you have a loop ;-)
[edit: changed explanation to numbered list SGaist]
-
Now my program work nice!Thank you for detailed ansewer:)
-
I'm glad to hear that. Happy coding :-)
-
Just as a note for this circular dependency as it can happen quite easy.
To resolve it often we use a class forward which is simply
class MyX;
and we don't include its header ( MyX header goes to the .cpp instead )
then in other class in same file
class Other {
MyX * someX;
}With the forward class, we are allowed to declare a pointer to that type (MyX) without compiler wanting to see the full header.
This only works for pointers and references as else it will want to see the full header as the compiler needs
to check other things when not a pointer.