Does not name a type error
-
wrote on 22 Aug 2016, 19:02 last edited by
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; }
-
wrote on 22 Aug 2016, 19:26 last edited by
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.
-
wrote on 23 Aug 2016, 06:46 last edited by
It is difficult to understand.
Can you show me in code?
Thanks -
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/4