“Field has incomplete type” error

There is an error in my header file:

field "ui" has incomplete type.

I have tried making ui a pointer, but that doesn’t work. I don’t think I need to do that because I have already defined my MainWindowClass in the namespace Ui. 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

Error: Field has an incomplete type

quaternion.h:15: error: field ‘v’ has incomplete type Hi! I am stuck on an error that I cannot seem to solve. Below is my code: #ifndef QUATERNION_H #define QUATERNION_H #include vec3.h class Vec3;

field has incomplete type

I got error of field has incomplete type but nor sure why. Can someone please help? Thanks! g++ -pipe -W -Wall -fopenmp -ggdb3 -DDEBUG -fno-omit-frame-pointer -c -o test.o ../../src/test.cc In file

Field has incomplete type?

So I have to write this geometry project in which I have many header files. The program gives me this mistake upon compilation : field ‘p’ has incomplete type. I think that the problem is that the fil

C: field has incomplete type

I apologise if this has been previously asked. I am getting the following error while compiling through make: …/inc/intModIp.h:418: error: field ‘cnc_id’ has incomplete type ../inc/intModIp.h:419:

Qt: field has incomplete type

Can’t compile my class. Getting error: error: field ‘filename’ has incomplete type If I change QString filename to QString *filename, error goes off.. but I need to have QString filename. process.h: #

Field has an Incomplete type?

I am getting an error regarding a struct element. struct trie { char ch; bool isEnd; struct trie arr[4]; struct trie *next; }; Error: error: field ‘arr’ has incomplete type

C++ error: field has incomplete type ‘int []’

I’m making a virtual machine in C++ and I’ve run into this error, error: field has incomplete type ‘int []’ int instrarr[]; I have absolutely no idea what is wrong with the int array. Can someone tak

field has incomplete type C++

I am trying to make class that has a member of type of it’s enclosing class but I get an error saying field has incomplete type. here is an example class List { public: List (int element, List rest) {

Type does not name a type or error: field has incomplete type

Im not sure why I am getting this error…error: field ‘config’ has incomplete type. I have tried to do forward declaration and including the headers using #include…Im just trying to include fConf

Field ‘permission_type’ has incomplete type

When I try to compile one I get this error: In file included from arch/arm/mach-msm/board-htcleo.c:81:0: include/linux/ion.h:192:27: error: field ‘permission_type’ has incomplete type How can I solve

Answers

The problem is that your ui property uses a forward declaration of class Ui::MainWindowClass, hence the “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. It’s a forward declaration, meaning that the class will exist at some point, at link time.
Basically, it just tells the compiler that the type will exist, and that it shouldn’t warn about it.

But the class has to be defined somewhere.

Note this can only work if you have a pointer to such a type.
You can’t have a statically allocated instance of an incomplete type.

So either you actually want an incomplete type, and then you should declare your ui member 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's an incomplete type
        Ui::MainWindowClass * ui;
};

Or you want a statically allocated instance of Ui::MainWindowClass, and then it needs to be declared. You can do it in another header file (usually, there’s one header file per class).
But simply 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 declarations. First uses a forward declaration, while the second one actually declares the class (here with no properties nor methods).

You are using a forward declaration for the type MainWindowClass. That’s fine, but it also means that you can only declare a pointer or reference to that type. Otherwise the compiler has no idea how to allocate the parent object as it doesn’t know the size of the forward declared type (or if it actually has a parameterless constructor, etc.)

So, you either want:

// forward declaration, details unknown
class A;

class B {
  A *a;  // pointer to A, ok
};

Or, if you can’t use a pointer or reference….

// declaration of A
#include "A.h"

class B {
  A a;  // ok, declaration of A is known
};

At some point, the compiler needs to know the details of A.

If you are only storing a pointer to A then it doesn’t need those details when you declare B. It needs them at some point (whenever you actually dereference the pointer to A), which will likely be in the implementation file, where you will need to include the header which contains the declaration of the class A.

// B.h
// 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 "B.h"
#include "A.h"  // declaration of A

B::foo() {
    // here we need to know the declaration of A
    a->whatever();
}