QT两个类互相访问
Qt two classes access each other
假设我有一个从 QMainWindow
继承的 Class A
和一个 Class B
。代码是这样:
在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
在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
,很容易在Class A
中获得Class B
元素,但是我也想在Class B
中获得Class A
元素(我希望这两个Class
可以互相访问),我尝试过的代码无效。这是因为Class A
从QMainWindow
继承吗?
为了实现这一目标,我该怎么办?
谢谢。
尝试将B::getNumA()
移至实现文件。因此,您会有
// b.cpp
#include "b.h"
#include "a.h"
void b::getNumA() {
qDebug() << testingA->tryingNumA; //did not work, error: invalid use of incomplete type 'class A'
}
目的是打破标题之间的圆形依赖性。
相关文章:
- 在类 A 中创建类型为 B 类的向量 - 访问数据 [C++] [成员在两个类中都是私有的]
- 两个抽象类,派生自同一个基类.如何访问从一个抽象类到另一个抽象类的指针
- 交换两个字符串时访问正确的内存时,我遇到了分段错误
- 两个类之间的交叉访问
- 从两个不同类继承的非虚拟基类的访问成员
- 通过两个下标访问数组成员
- 如何合并两个双重链接列表(访问下一个链接)
- 如何在循环中循环访问两个向量
- 在CPP中互相访问的两个结构
- QT两个类互相访问
- 使用Astmatcher获得至少两个相同访问说明符的课程
- 使用原子指令确保映射访问安全,是否可以使用两个不同的原子对指令进行重新排序
- 访问对象 B 中对象 C 的方法,这两个方法均由对象 A 创建
- 在C 中,可以从第三个共享对象访问其他两个不同共享对象
- 如何在C++中比较两个非随机访问迭代器
- 两个具有相同价值的枚举,如何访问它们
- 基于两个程序之间的事件触发器访问数据(内存)
- 对关键部分的两个进程访问出错
- 访问两个实例
- C++从基类访问两个类组成中的私有成员