“字段具有不完全类型”错误 [英] "Field has incomplete type" error

查看:304
本文介绍了“字段具有不完全类型”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的头文件中有错误:

field "ui" has incomplete type.



我尝试了 ui 但这不工作。我不认为我需要这样做,因为我已经在命名空间 Ui 中定义了 MainWindowClass 。这是我的 mainwindow.h

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


推荐答案

您正在使用 MainWindowClass 类型的向前声明。这很好,但它也意味着你只能声明一个指针或引用的类型。否则编译器不知道如何分配父对象,因为它不知道前向声明类型的大小(或者如果它实际上有一个无参数的构造函数等)。

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

所以,你要么:

// 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
};

有时,编译器需要知道 A

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

如果您只存储指向 A 的指针,那么当您声明 B 。它需要它们在某些时候(当你实际上将指针解引用 A )时,这可能在实现文件中,您将需要包含包含 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();
}

这篇关于“字段具有不完全类型”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆