WJuz WJuz - 3 months ago 16
C++ Question

myClass not declared in this scope

I make a project of music player in Qt based on QWidget class
and using widget.ui form.

I want to check that user will not add two labels with the same text, so I'm trying to add field: QList labelsList;
in my widget.h file.
(every time user add label, than: labelsList.append(label), and before he can add this label program would iterate through labelsList and check if in list exists label with patricular text).

Although "myqlabel.h" is included, compiler says that 'myQLabel' was not declared in this scope... I don't know why. Kind of weird for me, but maybe I lack/forget some basic knowledge... ;/

Thanks for help!

Code (just needed fragments) below:

widget.h file:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "myqlabel.h"

#include <QList>
#include <QFormLayout>

#include <QSqlDatabase>
#include <QtSql>

#include <QMediaPlayer>


namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();


// to check if label with input text already exists:

// HERE OUR BAD FIELD:

QList<myQLabel*> labelsList;



private:
Ui::Widget *ui;
QMediaPlayer player;

qint64 duration;

};

#endif // WIDGET_H


myqlabel.h file:

#ifndef MYQLABEL_H
#define MYQLABEL_H

#include <QLabel>
#include "widget.h"
#include "ui_widget.h"

#include <QFormLayout>
#include <QMouseEvent>

class myQLabel : public QLabel {

Q_OBJECT
public:

myQLabel(QString& text, QFormLayout* parent = 0) : QLabel(text){
setAcceptDrops(true);
position = amount;
this->parent = parent;

labelsList.append(this);
}



};


#endif // MYQLABEL_H

Answer

You have a circular dependency in your includes. This means that one of the headers is not seen depending on the order in which they are first included (due to the include guards).

Remove #include "myqlabel.h" from widget.h and add it to widget.cpp. Then forward declare myQLabel by adding

class myQLabel;

to widget.h.

Comments