Answer the question
In order to leave comments, you need to log in
Two classes refer to each other and lie in different .h files (without .cpp). How to do?
Here is an example sketched:
//parent.h
#pragma once
#include <QObject>
#include "child.h"
class Parent : public QObject {
Q_OBJECT
public:
Parent() : QObject()
{
child = new Child(this);
}
private:
Child *child;
};
//child.h
#pragma once
#include <QObject>
#include "parent.h"
class Parent;
class Child : public QObject {
Q_OBJECT
public:
Child(Parent *_parent) : QObject(_parent), parentPtr(_parent)
{}
private:
Parent *parentPtr;
};
C:\Users\Winner\Documents\example\child.h:11: ошибка: no matching function for call to 'QObject::QObject(Parent*&)'
Child(Parent *_parent) : QObject(_parent), parentPtr(_parent)
^
Answer the question
In order to leave comments, you need to log in
You have 2 classes that refer to each other. From an architectural point of view, this situation is called tight coupling, and you need pretty good reasons to allow it. Tell us more about what Parent and Child do, maybe you can refactor the code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question