我正在嘗試使用 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;
}
感謝您的幫助 !!
uj5u.com熱心網友回復:
您正在宣告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
注意:雖然將類定義和成員函式的實作放在Ui命名空間中將使其編譯,但您正在About_display為每個About_display創建的遞回創建一個新的。我懷疑您應該使用QDialogs 建構式并洗掉該About_display *ui;成員。
標題:
#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檔案中實作它們。
uj5u.com熱心網友回復:
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
{
// ...
};
您需要在命名空間中包含整個類定義:
namespace Ui
{
class About_display // NOW it is in namespace UI
: public QDialog
{
// ...
};
} // only here the namespace closes
您需要宣告類內的所有成員;但是您不必提供完整的定義;您可以在外部提供這些,如下例所示:
namespace Ui
{
class Demo
{
void demo(); // only declaration
};
}
// full definition – usually in .cpp file
void Ui::Demo::demo()
{
}
然而,遞回地創建子對話框將無休止地遞回,直到在某個時刻用完堆疊(-> 堆疊溢位...):
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...
你為什么需要那個孩子?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/382087.html
上一篇:制作一個在唯一時自動檢測的功能
