Let's say I have a Class A
which inherits from QMainWindow
, and a Class B
. The code is like this:
In a.h
:
#ifndef A_H
#define A_H
#include <QMainWindow>
#include "b.h"
class A : public QMainWindow
{
Q_OBJECT
public:
A(QWidget *parent = 0);
~A();
B TestingB;
int tryingNumA = 0;
void getNumB() {
qDebug() << TestingB.tryingNumB; //worked
}
};
#endif // A_H
In b.h
:
#ifndef B_H
#define B_H
#include <QDebug>
class A;
class B
{
public:
B();
int tryingNumB = 1;
A *testingA;
void getNumA() {
qDebug() << testingA->tryingNumA; //did not work, error: invalid use of incomplete type 'class A'
}
};
#endif // B_H
And it is easy to get Class B
elements in Class A
, but I also want to get Class A
elements in Class B
(I want these two Class
can access each other), the code I tried did not work. Is that the reason because Class A
inherits from QMainWindow
?
In order to achieve that, what can I do?
Thanks.