Curnelious Curnelious - 4 months ago 17
C++ Question

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 ?

Answer

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
};