Ticki
Ticki

Reputation: 15

How to fix a circular dependency?

I have some compilation error in Qt because of a circular dependency but I don't know how to fix it.

Here are the code samples:

QMdiSubWindowMod.h:

#include <QtWidgets/QtWidgets>
[...]
#include "fenetreedition.h"

class QMdiSubWindowMod : public QMdiSubWindow
{
Q_OBJECT
public:
    explicit QMdiSubWindowMod(QWidget * parent = 0, Qt::WindowFlags flags = 0);
    [...]
    void getPtrFenetreEdition(FenetreEdition* fen); //get error here in the second case
    ~QMdiSubWindowMod();
private:
    [...]
    void closeEvent(QCloseEvent *event);
    FenetreEdition *m_ptrFenetreEdition;
};

QMdiSubWindowMod.cpp:

#include "qmdisubwindowmod.h"

QMdiSubWindowMod::QMdiSubWindowMod(QWidget * parent, Qt::WindowFlags flags)
    : QMdiSubWindow(parent, flags)
{
}

QMdiSubWindowMod::~QMdiSubWindowMod()
{
}

void QMdiSubWindowMod::closeEvent (QCloseEvent *event)
{
    [...]
    m_ptrFenetreEdition->onSubWindowClose();
}

[...]

void QMdiSubWindowMod::getPtrFenetreEdition(FenetreEdition* fen)
{
    m_ptrFenetreEdition = fen; //and here too for the second case
}

The way I call it:

FenetreEdition.h:

#include "qmdisubwindowmod.h"
#include <QtWidgets/QtWidgets>
[...]

FenetreEdition.cpp:

QMdiSubWindowMod *onglet = new QMdiSubWindowMod(m_centralArea);
[...]
onglet->getPtrFenetreEdition(&this);

Here are the error showed in the compilator (Qt Creator):

C2061: Syntax error: identifier 'FenetreEdition' //on method void getPtrFenetreEdition(FenetreEdition* fen);

C2143: Syntax error: missing ';' before '*' //on FenetreEdition *m_ptrFenetreEdition;

C4430: missing type specifier - int assumed. note c++ does not support default-int //on FenetreEdition *m_ptrFenetreEdition;

I can't remove the include in FenetreEdition because I need this class to create the QMdiSubWindowMod and i can't remove the include in QMdiSubWindowMod because I need the FenetreEdition pointer to call method on some points.

How to fix it? Thank in advance for your answers!

Upvotes: 0

Views: 1225

Answers (1)

clcto
clcto

Reputation: 9648

It appears you only use a pointer to FenetreEdition in QMdiSubWindowMod.h. In this case you can forward declare the class instead of including the header. Then in the .cpp file you include the header.

QMdiSubWindowMod.h:

#include <QtWidgets/QtWidgets>
 [...]
class FenetreEdition;

Upvotes: 5

Related Questions