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. Does not name a type error
QtWS25 Last Chance

Does not name a type error

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++mainwindowqt 5.7
4 Posts 3 Posters 5.8k Views
  • 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.
  • ronyNSR Offline
    ronyNSR Offline
    ronyNS
    wrote on last edited by
    #1

    Error : 'employeeinfo' does not name a type
    I tried many different ways to solve it but it keeps on coming
    The error goes when i remove #include "mainwindow.h" from employeeinfo.h
    Please help.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include<QLabel>
    #include <QMainWindow>
    #include<QPushButton>
    #include<QSqlDatabase>
    #include "employeeinfo.h"
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    protected:
    
    private slots:
    
    private:
        Ui::MainWindow *ui;
        employeeinfo *emp; <- error
    
    };
    
    #endif // MAINWINDOW_H
    

    employeeinfo.h

    #ifndef EMPLOYEEINFO_H
    #define EMPLOYEEINFO_H
    #include <QMainWindow>
    #include "mainwindow.h"
    
    namespace Ui {
    class employeeinfo;
    }
    
    1 Reply Last reply
    0
    • W Offline
      W Offline
      Wurgl
      wrote on last edited by
      #2

      In short: One file include the other one. This usually does not work as expected.

      There are these guards "EMPLOYEEINFO_H" and MAINWINDOW_H which prevent an endless recursion, but they have the side effect, that another include causes zero lines to get included.

      Try it by hand, start with employeeinfo.h. There you include mainwindow.h, but you include it before the type employeeinfo is known to the compiler. in mainwindow.h you try to include employeeinfo.h, but since you started with that file, the preprocessor already knows that symbol EMPLOYEEINFO_H and does nothing, so you end up with that error.

      Solution: If you have just a pointer, just a reference, say: If you just need to know the name of the type, then use a forward definition and do not include the file with the full definition.

      1 Reply Last reply
      1
      • ronyNSR Offline
        ronyNSR Offline
        ronyNS
        wrote on last edited by
        #3

        It is difficult to understand.
        Can you show me in code?
        Thanks

        mrjjM 1 Reply Last reply
        0
        • ronyNSR ronyNS

          It is difficult to understand.
          Can you show me in code?
          Thanks

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

          hi

          in mainwindow.h

          instead of
          #include "employeeinfo.h" << this already included so not working here.

          we do
          class employeeinfo; <<< this is a forward.

          Since you do
          employeeinfo *emp;

          a forward is allowed. ( that is what @Wurgl explained , much better than this :)

          1 Reply Last reply
          0

          • Login

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