C++ 中的循环依赖援助
我有一个类似于以下代码的代码,但我不知道如何使其工作。
我已经搜索过它,看起来与循环依赖有关,但现在,我尝试了一些示例,但仅适用于依赖项 2。
相反,这个,我有很多“Ctrl”类类依赖于它们(CtrlA 和 CtrlB 相互依赖,Ax 类需要两个 Ctrl),而且其中一些类也需要 Ctrl 文件(CtrlA 需要 Ax 类)。另外,我有一个继承类(A2 继承 A3)。
CtrlA.h
#ifndef CTRLA
#define CTRLA
#include "CtrlB.h"
#include "A1.h"
class CtrlB;
class A1;
class CtrlA{
protected:
A1 x;
public:
void op1(CtrlB b){
a.op1(this, b);
}
void op2(){}
};
#endif
CtrlB.h
#ifndef CTRLB
#define CTRLB
#include "CtrlA.h"
class CtrlA;
class CtrlB{
protected:
public:
void op1(){}
void op2(CtrlA a){
a.op1(this);
}
};
#endif
A1.h
#ifndef A1
#define A1
#include "CtrlA.h"
#include "CtrlB.h"
#include "A2.h"
class CtrlA;
class CtrlB;
class A1{
protected:
A2 x1;
public:
void op1(CtrlA a, CtrlB b){
x1.op1(this, b);
}
};
#endif
A2.h
#ifndef A2
#define A2
#include "CtrlA.h"
#include "CtrlB.h"
#include "A3.h"
class CtrlA;
class CtrlB;
class A2:public A3{
protected:
public:
void op1(CtrlA a, CtrlB b){
a.op2();
b.op1();
}
};
#endif
A3.h
#ifndef A3
#define A3
#include "CtrlA.h"
#include "CtrlB.h"
class CtrlA;
class CtrlB;
class A3{
protected:
public:
virtual void op1(CtrlA a, CtrlB b) = 0;
};
#endif
main.cpp
#include "CtrlA.h"
#include "CtrlB.h"
int main(){
int i;
}
如果有人可以帮助我更正代码以便它可以工作,我将非常感激。
I have a code similar to the following one, but i cant get how to make it work.
I've searched for it and looks to be something about circular dependency, but for now, I've tried with some examples, but only work with a dependency of 2.
Instead, this one, I have the "Ctrl" classes that many classes are dependent of them (CtrlA and CtrlB are mutially dependent and Ax classes needs both Ctrl), but also the Ctrl files are needed of some of those clases (CtrlA needs Ax classes). Also, I have an inherited class (A2 inherits A3).
CtrlA.h
#ifndef CTRLA
#define CTRLA
#include "CtrlB.h"
#include "A1.h"
class CtrlB;
class A1;
class CtrlA{
protected:
A1 x;
public:
void op1(CtrlB b){
a.op1(this, b);
}
void op2(){}
};
#endif
CtrlB.h
#ifndef CTRLB
#define CTRLB
#include "CtrlA.h"
class CtrlA;
class CtrlB{
protected:
public:
void op1(){}
void op2(CtrlA a){
a.op1(this);
}
};
#endif
A1.h
#ifndef A1
#define A1
#include "CtrlA.h"
#include "CtrlB.h"
#include "A2.h"
class CtrlA;
class CtrlB;
class A1{
protected:
A2 x1;
public:
void op1(CtrlA a, CtrlB b){
x1.op1(this, b);
}
};
#endif
A2.h
#ifndef A2
#define A2
#include "CtrlA.h"
#include "CtrlB.h"
#include "A3.h"
class CtrlA;
class CtrlB;
class A2:public A3{
protected:
public:
void op1(CtrlA a, CtrlB b){
a.op2();
b.op1();
}
};
#endif
A3.h
#ifndef A3
#define A3
#include "CtrlA.h"
#include "CtrlB.h"
class CtrlA;
class CtrlB;
class A3{
protected:
public:
virtual void op1(CtrlA a, CtrlB b) = 0;
};
#endif
main.cpp
#include "CtrlA.h"
#include "CtrlB.h"
int main(){
int i;
}
I would be very grateful if someone could help me to correct the code so it can work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 CtrlA.h、CtrlB.h、A1.h 和 A3.h,如果您使用前向声明(您有点这样做了)并使用引用或指针(您没有这样做),则不需要 #include 任何内容:
< strong>CtrlA.h
A1.h
但是对于 A2.h,您将从 A3 继承,因此您必须 #include A3.h
A2.h< /strong>
那留下 main.cpp,您需要在其中包含所有内容:
main.cpp
希望有帮助! 这里有一个快速参考前向声明以及何时/如何使用它。
编辑:感谢巴勃罗指出我的错误。您不能使用前向声明的类作为成员对象,只能使用引用或指针。我已将上面的示例更改为使用指针。
For CtrlA.h, CtrlB.h, A1.h, and A3.h you don't need to #include anything if you use forward declaration (which you sorta did) and use references or pointers (which you did not):
CtrlA.h
A1.h
But with A2.h, you're inheriting from A3, so you'll have to #include A3.h
A2.h
And that leaves main.cpp, where you'll want to include 'em all:
main.cpp
Hope that helps! Here is a quick reference to forward declaration and when/how to use it.
Edit: Thanks to Pablo for pointing out my error. You cannot use forward declared classes as member objects, only references or pointers. I've changed the above examples to use pointers.
我的建议:使用更多的指针和尽可能多的前向声明(并避免在 .h 文件中 #include 其他标头。#include 它们在实现所在的 .cpp 文件中是必需的。)
因此
CtrlA.h
您不能将不完整的类型声明为成员。
您将获得的一些有用信息找到在这个问题的第二个答案
My advice: use more pointers and as many forward declarations as possible (and avoid #including other headers in .h files. #include them in the .cpp file where the implementation is needed.)
So
CtrlA.h
You cannot declare a incomplete type as an member.
The bit of useful information you'll find in the 2nd answer to this question