Does not name a type error

  • 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.


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


    #ifndef EMPLOYEEINFO_H
    #define EMPLOYEEINFO_H
    #include <QMainWindow>
    #include "mainwindow.h"
    namespace Ui {
    class employeeinfo;

  • 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.

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

  • Lifetime Qt Champion


    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 :)