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

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.
  • R Offline
    R Offline
    ronyNS
    wrote on 22 Aug 2016, 19:02 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 22 Aug 2016, 19:26 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
      • R Offline
        R Offline
        ronyNS
        wrote on 23 Aug 2016, 06:46 last edited by
        #3

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

        M 1 Reply Last reply 23 Aug 2016, 06:56
        0
        • R ronyNS
          23 Aug 2016, 06:46

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 23 Aug 2016, 06:56 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

          4/4

          23 Aug 2016, 06:56

          • Login

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