Hey guys.
I'm new to object orientated programming and therefore
have an inheritance issue I cannot resolve.
The actual problem is, I want to have a class (Mother), which contains
a pointer to another base class (Child) as a member.
Different types of child classes (Son,Daughter) should be assignable to
this base class pointer.
Unfortunately I don't succeed to resolve the preprocessor case lock, with forward declaration of the classes.
With the attached file structure (attached further down),
I get the following errors:
>>son.h: error: expected class-name before ‘{’ token
>>daughter.h: error: expected class-name before ‘{’ token
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
//-------------------------------------------------------
//file mother.h
#ifndef MOTHER_H
#define MOTHER_H
#include "child.h"
class Mother
{
Child* childptr;
public:
void setChildren(const char* childtype){
if ...
this->childptr=new Son();
childptr[0].setMother(this);
...
}
}
#endif
//-------------------------------------------------------
//file child.h
#ifndef CHILD_H
#define CHILD_H
class Mother; //Forward declaration
class Child
{
protected:
Mother* childptr;
public:
virtual void setMother(Mother* motherptr);
}
#endif
//-------------------------------------------------------
//file daughter.h
#ifndef DAUGHTER_H
#define DAUGHTER_H
#include "mother.h"
#include "child.h"
class Daughter : public Child
{
public:
void setMother(Mother* motherptr);
}
#endif
//-------------------------------------------------------
//file son.h
#ifndef SON_H
#define SON_H
#include "mother.h"
#include "child.h"
class Son : public Child
{
public:
void setMother(Mother* motherptr);
}
#endif
//-------------------------------------------------------
//file main.h
#include mother.h
int main()
{
Mother mymother;
mymother.setChildren("Son");
}
| |
Do you have any ideas, how to solve this problem?
Thanks a lot in advance