2016-08-03 120 views
0

后有点搜索我才知道,我可以这样调用父方法:在父类中调用一个方法

基类:

class Base 
{ 


public: 

    Base(); 
    child *children; // instance of the child is needed on the base 
    float theDelegate(char *arg); 

然后子类:

class child: public Base //**** problem 
{ 


public: 
... 

但是,当我试图添加public Base行时,我收到一个错误,他不知道Base

于是我包括basechild,与此:

#include "Base.hpp" 

这时候孩子可以看到父,但权当我包括在childbase我就得到一个错误父母,因为他们包括彼此

child *children; - unknown type name child - appear only if I include parent in the child 

我在这里做错了什么?应该怎么做?

+2

向前声明? –

+0

具体答案?根据这里的问题,这个问题,我做了他们所展示的。 – Curnelious

+0

上课的孩子;在你的基地 –

回答

5

使用前声明:

文件Base.hpp:

class Child; // forward declaration 

class Base { 
public: 
    Child* child; 
    // whatever 
}; 

文件Child.hpp:

#include "Base.hpp" 

class Child : public Base { 
    // whatever 
}; 
+0

而不是包括在基地的孩子? – Curnelious

+2

@Curnelious是的,而是。编译器不需要知道任何关于“Child”的实现,因为它只需要指向此翻译单元中的“Child”。 –

+0

那么这将无法正常工作。这样做会造成更多的错误 – Curnelious