我想从类A调用Foo()函数,通过另一个类的指针 (我不能包含其他类的头文件,因为我在另一个类中执行该函数,所以这将导致一个,,包含本身'错误)我该怎么做?类互相调用函数(包括异常)
//a.h
class X; // here I can't include "x.h", so forward declaration, but that results incomplete type
class A
{
X* ptr;
void Example()
{
X->Foo(); // it can't be use, casue ,,incomplete type is not allowed''
}
void Print();
}
//x.h
#include "a.h"
class X
{
A* ptrarray[];
void Foo();
void List()
{
for (i...)
ptrarray[i]->Print(); // ok, works fine
}
}
我怎样才能解决这个问题搞定了,你能给我什么建议?
使用标头警卫:http://en.wikipedia.org/wiki/Include_guard – TheDarkKnight 2013-05-10 11:00:44
我试过了,但没有工作......或者请在这个例子中向我展示正确的用法。谢谢 – user2369698 2013-05-10 11:02:07
将你的文件分割成.h和.cpp文件,然后把这个实现放到cpp文件中。这样,你只需要转发声明类而不在.h文件中包含头文件。 – TheDarkKnight 2013-05-10 11:06:21