program story

"필드에 불완전한 유형이 있습니다"오류

inputbox 2020. 11. 5. 08:00
반응형

"필드에 불완전한 유형이 있습니다"오류


내 헤더 파일에 오류가 있습니다.

field "ui" has incomplete type.

ui포인터를 만들려고했지만 작동하지 않습니다. 이미 MainWindowClass네임 스페이스에 정의했기 때문에 그렇게 할 필요가 없다고 생각 합니다 Ui. 이것은 내 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

유형에 대해 앞으로 선언을 사용하고 있습니다 MainWindowClass. 괜찮지 만 해당 유형에 대한 포인터 또는 참조 만 선언 할 수 있음을 의미하기도합니다. 그렇지 않으면 컴파일러는 앞으로 선언 된 유형의 크기를 알지 못하기 때문에 부모 객체를 할당하는 방법을 알지 못합니다 (또는 실제로 매개 변수가없는 생성자가 있는지 등).

따라서 다음 중 하나를 원합니다.

// forward declaration, details unknown
class A;

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

또는 포인터 나 참조를 사용할 수없는 경우 ...

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

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

어느 시점에서 컴파일러는의 세부 정보를 알아야합니다 A.

에 대한 포인터 만 저장하는 A경우 선언 할 때 해당 세부 정보가 필요하지 않습니다 B. 어느 시점에서 (실제로 포인터를 역 참조 할 때마다 A) 구현 파일에있을 가능성이 높으며 클래스 선언을 포함하는 헤더를 포함해야합니다 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();
}

문제는 ui속성 이 class 정방향 선언사용 Ui::MainWindowClass하므로 "불완전한 유형"오류가 발생한다는 것입니다.

이 클래스가 선언 된 헤더 파일을 포함하면 문제가 해결됩니다.

편집하다

귀하의 의견에 따라 다음 코드 :

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

참고URL : https://stackoverflow.com/questions/12466055/field-has-incomplete-type-error

반응형