[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
        MainWindow(QWidget *parent = 0);
        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
        Sprite(QObject *parent = 0,string file = "default.bmp");
        QImage getSprite();
    public slots:
        QImage image;
        int x;
        int y;
    #endif // SPRITE_H

    I will be grateful for any advices!

  • Moderators

    You should remove inclusion of mainwindow.h from sprite.h: you have introduced a circular dependency:

    1. The compiler reads mainwindow and encounters sprite.h include
    2. It proceeds to sprite.h, but finds mainwindow.h include there
    3. It goes back to mainwindow.h, as instructed
    4. And you have a loop ;-)

    [edit: changed explanation to numbered list SGaist]

  • Now my program work nice!Thank you for detailed ansewer:)

  • Moderators

    I'm glad to hear that. Happy coding :-)

  • Thanks Sir

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.