Error "Field is incomplete type"

There is an error in my header file:

field "ui" has incomplete type. 

I tried to make the ui pointer a pointer, but that will not work. I don’t think I need to do this because I already defined my MainWindowClass in the ui namespace. This is my mainwindow.h :

 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtGui/QMainWindow> #include "ui_mainwindow.h" namespace Ui { class MainWindowClass; } class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0, Qt::WFlags flags=0); ~MainWindow(); public slots: void slideValue(int); private: Ui::MainWindowClass ui; //error line }; #endif // MAINWINDOW_H 
+52
c ++
Sep 17 '12 at 19:41
source share
2 answers

You are using a forward declaration for the type MainWindowClass . This is fine, but it also means that you can only declare a pointer or reference to this type. Otherwise, the compiler has no idea how to distribute the parent object, since it does not know the size of the direct declared type (or if it actually has a constructor without parameters, etc.)

So you either want:

 // forward declaration, details unknown class A; class B { A *a; // pointer to A, ok }; 

Or, if you cannot use a pointer or link ....

 // declaration of A #include "Ah" class B { A a; // ok, declaration of A is known }; 

At some point, the compiler should know the details of A

If you keep a pointer to A , then it does not need these details when declaring B He needs them at some point (whenever you are actually looking for a pointer to A ), which will most likely be in the implementation file, where you will need to include a header that contains the class A declaration.

 // Bh // header file // forward declaration, details unknown class A; class B { public: void foo(); private: A *a; // pointer to A, ok }; // B.cpp // implementation file #include "Bh" #include "Ah" // declaration of A B::foo() { // here we need to know the declaration of A a->whatever(); } 
+71
Sep 17
source share

The problem is that your ui property uses the declaration of the Ui::MainWindowClass , therefore, an "incomplete type" error.

Including the header file in which this class is declared will fix the problem.

EDIT

Based on your comment, the following code:

 namespace Ui { class MainWindowClass; } 

does NOT declare a class. This is a forward declaration , which means that the class will exist at some point during the link.
Basically, it simply tells the compiler that the type will exist and that it should not warn about it.

But the class must be defined somewhere .

Please note that this can only work if you have a pointer .
You cannot have a statically allocated instance of an incomplete type.

So, either you really want an incomplete type, and then you have to declare your ui -rent as a pointer:

 namespace Ui { // Forward declaration - Class will have to exist at link time class MainWindowClass; } class MainWindow : public QMainWindow { private: // Member needs to be a pointer, as it an incomplete type Ui::MainWindowClass * ui; }; 

Or you need a statically allocated instance of Ui::MainWindowClass , and then it must be declared. You can do this in another header file (usually there is one header file for each class).
But just changing the code to:

 namespace Ui { // Real class declaration - May/Should be in a specific header file class MainWindowClass {}; } class MainWindow : public QMainWindow { private: // Member can be statically allocated, as the type is complete Ui::MainWindowClass ui; }; 

will also work.

Note the difference between the two ads. First, a forward declaration is used, and the second actually declares the class (here without properties and methods).

+11
Sep 17 '12 at 19:44
source share



All Articles