Qt World Summit: Submit your Presentation

[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

  • Works as charms! right reasoning was required, thanks.

  • Lifetime Qt Champion


    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.

Log in to reply