【发布时间】:2013-01-30 10:58:08
【问题描述】:
我正在编写由大量 .h 和 .c 文件组成的 C++ 大型代码。
主要问题是由一对应该相互链接的类引起的。 由于软件架构中的声明需要,第一个类(命名为 A)在“上层”类中初始化。
所以我们得到了类似的东西:
#include A.h
class mainClass{
...
A a;
...
}
A.h 看起来像:
#ifndef A_H
#define A_H
#include B.h
class A{
A();
fooA();
...
private:
B b;
...
}
#endif
A.cpp 看起来像:
#include B.h
#include A.h
...
A::A(){
...
b(this) //here I get the first error that follows
...
}
A::fooA(){//do somthing}
为了避免在第二个类中包含相互的头文件(让它成为 B),我使用了前向声明和指向 A 类的指针 var。
B.h 看起来像:
#ifndef B_H
#define B_H
class A; //Forward declaration to A
class B{
B()
B(A* const t)
fooB();
A* a; //pointer to A object
}
B.cpp 看起来像:
#include B.h
B::B(){
//default constructor. Do Nothing
}
B::B(A* const t){
this->a=t //constructor that set the pointer to the object of type A
}
B::fooB(){
a->fooA(); //here i get the second error that follows
}
现在,如果在我的 Makefile 中我在 B 之前链接 A,我得到编译错误:
//First error. See code above for line numbers
error: no match for call to ‘(B) (A* const)’
另一方面,如果我在 A 之前链接 B,我会得到编译错误:
//Second error. see code above for line numbers
error: invalid use of incomplete type ‘struct A’
_B.h:'line of the forward declaration': error: forward declaration of ‘struct A’
我必须承认我对 c++ 还很陌生,所以我不明白我错在哪里。
编辑
现在我正在使用解决方案:
- 使用包含防护
- 前向声明A类,B.h中不包含A.h
- 在 A.cpp 和 B.cpp 中同时包含 B.h 和 A.h。始终在 A.h 之前包含 B.h
但我得到同样的错误:
error: no match for call to ‘(B) (A* const)'
会不会是构造函数重载问题?如果我删除该行
b(this)
编译工作正常。
已解决
如果 a 使用帮助函数在 B 中设置变量 A* a,则使用构造函数在编译期间一切正常。也许我需要更好地理解 C++ 中的构造函数重载。非常感谢。
【问题讨论】:
-
嗯,首先引起我注意的是:您的头文件缺少包括保护。 en.wikipedia.org/wiki/Include_guard
-
我为这个错误道歉。所有头文件都包含保护...再次抱歉...我正在尝试简化两个类中的大量代码,但我忘记在上面的代码中编写包含保护。
-
我编辑了帖子以更好地反映我的代码的真实状态。谢谢你的建议。