I am trying to implement my very first project of a mediaplayer using QT c , currently i have this issue where it says "error: allocation of incomplete type '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;
}
Thank you for the help !!
CodePudding user response:
You are declaring class Ui::About_display;
but defining class About_display
. Make sure the class
definition is in the Ui
namespace:
#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
And also in the .cpp
file:
#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
Note: While placing the class definition and implementation of the member functions in the Ui
namespace will make it compile, you are recursively creating a new About_display
for every About_display
you create. I suspect you should use the QDialog
s constructor and remove the About_display *ui;
member.
Header:
namespace Ui {
class About_display : public QDialog
{
Q_OBJECT
public:
using QDialog::QDialog; // add the QDialog constructors
};
The member functions you've defined in your original code are already covered by QDialog
so with what you've shown, you don't need to implement them in the .cpp
file.
CodePudding user response:
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
{
// ...
};
You need to include the entire class definition in the namespace:
namespace Ui
{
class About_display // NOW it is in namespace UI
: public QDialog
{
// ...
};
} // only here the namespace closes
You need to declare all the members inside the class; however you don't have to provide full definitions; these you can provide outside as in the following example:
namespace Ui
{
class Demo
{
void demo(); // only declaration
};
}
// full definition – usually in .cpp file
void Ui::Demo::demo()
{
}
However recursively creating a child dialog will recurse endlessly until at some point you run out of stack (-> stack overflow...):
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...
Why would you need that child at all?