简体   繁体   中英

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 .

So then I include base to the child , with this :

#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 .

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:

class Child; // forward declaration

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

File Child.hpp:

#include "Base.hpp"

class Child : public Base {
    // whatever
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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