Main.cpp的循环包括在C++ - 再次
#include "Test1.h"
#include "Test2.h"
int main(){
Test1 t1;
Test2 t2;
t1.process(t2);
t2.process(t1);
}
Test1.h
#ifndef TEST1
#define TEST1
#include "Test2.h"
class Test1 {
public:
void process(const Test2& t) {};
};
#endif // !TEST1
Test2.h
#ifndef TEST2
#define TEST2
#include "Test1.h"
class Test2 {
public:
void process(const Test1& t) {};
};
#endif // !TEST2
VS2012说:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '&'
error C2664: 'Test2::process' : cannot convert parameter 1 from 'Test1' to 'const int'
我敢肯定它是圆形包括问题又来了(我在一段时间遇到一次),但这次我不知道为什么不能做到这一点的编译。
注意:这些类只依赖于彼此的引用,它们的大小已知。是否因为包含卫士(#ifndef
),使得其中一个Test头包含另一个为空文件?
为什么你需要在所有的包括哪些内容?前缀声明在'* .h'中都是足够的。 – kennytm 2013-05-06 18:10:05
有数以百万计的重复。在发布之前花点功夫去研究这个问题。 – 2013-05-06 18:10:29
看[这里](http://stackoverflow.com/questions/553682/when-to-use-forward-declaration),很好的解释。在决定是转发声明还是包含时,可以将其用作经验法则。 – 2013-05-06 18:10:56