QT c++中不完整类型错误的分配
Allocation of incomplete type error in QT c++
我正在尝试使用 QT c++ 实现我的第一个媒体播放器项目,目前我遇到这个问题,它显示“错误:不完整类型的分配 'Ui::About_display'”
.h
#ifndef PLAYERFRAME_H
#define PLAYERFRAME_H
#include <QDialog>
namespace Ui {
class About_display;
}
class About_display : public QDialog
{
Q_OBJECT
public:
explicit About_display(QWidget *parent = nullptr);
~About_display();
private:
Ui::About_display *ui;
};
#endif // PLAYERFRAME_H
.cpp
include "playerframe.h"
#include "ui_about_display.h"
About_display::About_display(QWidget *parent) :
QDialog(parent),
ui(new Ui::About_display) ..> where error occurs
{
ui->setupUi(this);
}
About_display::~About_display()
{
delete ui;
}
感谢您的帮助!!
您正在声明 class Ui::About_display;
但定义 class About_display
。确保 class
定义在 Ui
命名空间中:
#ifndef PLAYERFRAME_H
#define PLAYERFRAME_H
#include <QDialog>
namespace Ui {
class About_display : public QDialog
{
Q_OBJECT
public:
explicit About_display(QWidget *parent = nullptr);
~About_display();
private:
About_display *ui; // `Ui::` not needed
};
} // namespace Ui
#endif // PLAYERFRAME_H
并且还在 .cpp
文件中:
#include "playerframe.h"
#include "ui_about_display.h"
namespace Ui {
About_display::About_display(QWidget *parent) :
QDialog(parent),
ui(new About_display) // `Ui::` not needed
{
ui->setupUi(this);
}
About_display::~About_display()
{
delete ui;
}
} // namespace Ui
注意:虽然将成员函数的 class 定义和实现放在 Ui
命名空间中将使其编译,但您正在递归地为每个 About_display
你创造。我怀疑您应该使用 QDialog
s 构造函数并删除 About_display *ui;
成员。
Header:
#ifndef PLAYERFRAME_H
#define PLAYERFRAME_H
#include <QDialog>
namespace Ui {
class About_display : public QDialog
{
Q_OBJECT
public:
using QDialog::QDialog; // add the QDialog constructors
};
#endif
您在原始代码中定义的成员函数已包含在 QDialog
中,因此对于您所展示的内容,您无需在 .cpp
文件中实现它们。
namespace Ui
{
class About_display; // declares a class Ui::About_display
} // however, namespace closes!
class About_display // so this is *another* class ::About_display in global namespace!
: public QDialog
{
// ...
};
您需要在命名空间中包含整个 class 定义:
namespace Ui
{
class About_display // NOW it is in namespace UI
: public QDialog
{
// ...
};
} // only here the namespace closes
您需要声明class内的所有成员;但是您不必提供完整的定义;这些您可以在外部提供,如下例所示:
namespace Ui
{
class Demo
{
void demo(); // only declaration
};
}
// full definition – usually in .cpp file
void Ui::Demo::demo()
{
}
然而,递归地创建一个 child 对话框将无休止地递归,直到在某个时候你 运行 出栈(-> 栈溢出...):
Ui::About_display::About_display(QWidget* parent)
: ui(new About_display()) // <- will create a child on its own, too
// that one will create yet another child,
// that one again, and again, and again...
你为什么需要 child?
我正在尝试使用 QT c++ 实现我的第一个媒体播放器项目,目前我遇到这个问题,它显示“错误:不完整类型的分配 'Ui::About_display'”
.h
#ifndef PLAYERFRAME_H
#define PLAYERFRAME_H
#include <QDialog>
namespace Ui {
class About_display;
}
class About_display : public QDialog
{
Q_OBJECT
public:
explicit About_display(QWidget *parent = nullptr);
~About_display();
private:
Ui::About_display *ui;
};
#endif // PLAYERFRAME_H
.cpp
include "playerframe.h"
#include "ui_about_display.h"
About_display::About_display(QWidget *parent) :
QDialog(parent),
ui(new Ui::About_display) ..> where error occurs
{
ui->setupUi(this);
}
About_display::~About_display()
{
delete ui;
}
感谢您的帮助!!
您正在声明 class Ui::About_display;
但定义 class About_display
。确保 class
定义在 Ui
命名空间中:
#ifndef PLAYERFRAME_H
#define PLAYERFRAME_H
#include <QDialog>
namespace Ui {
class About_display : public QDialog
{
Q_OBJECT
public:
explicit About_display(QWidget *parent = nullptr);
~About_display();
private:
About_display *ui; // `Ui::` not needed
};
} // namespace Ui
#endif // PLAYERFRAME_H
并且还在 .cpp
文件中:
#include "playerframe.h"
#include "ui_about_display.h"
namespace Ui {
About_display::About_display(QWidget *parent) :
QDialog(parent),
ui(new About_display) // `Ui::` not needed
{
ui->setupUi(this);
}
About_display::~About_display()
{
delete ui;
}
} // namespace Ui
注意:虽然将成员函数的 class 定义和实现放在 Ui
命名空间中将使其编译,但您正在递归地为每个 About_display
你创造。我怀疑您应该使用 QDialog
s 构造函数并删除 About_display *ui;
成员。
Header:
#ifndef PLAYERFRAME_H
#define PLAYERFRAME_H
#include <QDialog>
namespace Ui {
class About_display : public QDialog
{
Q_OBJECT
public:
using QDialog::QDialog; // add the QDialog constructors
};
#endif
您在原始代码中定义的成员函数已包含在 QDialog
中,因此对于您所展示的内容,您无需在 .cpp
文件中实现它们。
namespace Ui
{
class About_display; // declares a class Ui::About_display
} // however, namespace closes!
class About_display // so this is *another* class ::About_display in global namespace!
: public QDialog
{
// ...
};
您需要在命名空间中包含整个 class 定义:
namespace Ui
{
class About_display // NOW it is in namespace UI
: public QDialog
{
// ...
};
} // only here the namespace closes
您需要声明class内的所有成员;但是您不必提供完整的定义;这些您可以在外部提供,如下例所示:
namespace Ui
{
class Demo
{
void demo(); // only declaration
};
}
// full definition – usually in .cpp file
void Ui::Demo::demo()
{
}
然而,递归地创建一个 child 对话框将无休止地递归,直到在某个时候你 运行 出栈(-> 栈溢出...):
Ui::About_display::About_display(QWidget* parent)
: ui(new About_display()) // <- will create a child on its own, too
// that one will create yet another child,
// that one again, and again, and again...
你为什么需要 child?