Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Need Help: Error " 'CLASS' does not name a type"
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Need Help: Error " 'CLASS' does not name a type"

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 29.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GeorgePopov
    wrote on last edited by VRonin
    #1

    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!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by SGaist
      #2

      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]

      (Z(:^

      1 Reply Last reply
      7
      • G Offline
        G Offline
        GeorgePopov
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

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

          (Z(:^

          1 Reply Last reply
          1
          • SurajS Offline
            SurajS Offline
            Suraj
            wrote on last edited by
            #5

            Thanks Sir

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Samarth
              wrote on last edited by
              #6

              Works as charms! right reasoning was required, thanks.

              mrjjM 1 Reply Last reply
              0
              • S Samarth

                Works as charms! right reasoning was required, thanks.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Samarth

                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.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved