简体   繁体   English

在父类中调用方法

[英]Calling a method in a parent class

After a little bit searching I learned that I could call a parent method like this: 经过一番搜索后,我了解到可以像这样调用父方法:

Base class : 基类:

class Base
{


public:

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

Then child class : 然后孩子上课:

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


public:
...

But when I am trying to add the public Base line, I get an error that he does not know Base . 但是,当我尝试添加public Base行时,出现一个错误,他不知道Base

So then I include base to the child , with this : 因此,我将base包括在child base

#include "Base.hpp"

This time the child can see the parent ,but right when I include the base on the child I get an error on the parent because they include each other . 这次孩子可以看到父母,但是当我包括childbase时,我对父母却产生了错误,因为他们彼此包括了

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

What am I doing wrong here ? 我在这里做错了什么? How should it be done ? 应该怎么做?

Use forward-declaration: 使用前向声明:

File Base.hpp: 文件Base.hpp:

class Child; // forward declaration

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

File Child.hpp: 文件Child.hpp:

#include "Base.hpp"

class Child : public Base {
    // whatever
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM