Class inheritance and forward declaration problem

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
Last edited on
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

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
//-------------------------------------------------------
//file mother.h
#ifndef MOTHER_H
#include "child.h"
#include "daughter.h"
#include "son.h"
class Child; //Forward declaration
class Mutter
{
child* childptr;
public:
void setChildren(const char* childtype){
if ...
this->childptr=new Son();
childptr[0].setMother(this);
...
}
}
#endif
//-------------------------------------------------------
//file child.h
#ifndef CHILD_H
#include "mother.h"
class Mother; //Forward declaration
class Child
{
protected:
Mutter* childptr;
public:
virtual void setMother(); 
}
#endif
//-------------------------------------------------------
//file daughter.h
#ifndef DAUGHTER_H
#include "mother.h"
#include "child.h"
class Child; //Forward declaration
class Daughter : public Child
{
public:
void setMother(Mutter mutterptr); 
}
#endif
//-------------------------------------------------------
//file son.h
#ifndef SON_H
#include "mother.h"
#include "child.h"
class Child; //Forward declaration
class Son : public Child
{
public:
void setMother(Mutter mutterptr);
}
#endif
//-------------------------------------------------------
//file main.h
#include mother.h
int main()
{
Mother mymother;
mymother.setChildren("Son");
}


Line 10: You're declaring a pointer to child, but your capitalization is wrong.

Line 24: You're declaring Mother as forward, but your class name is Mutter.

Line 62: You're declaring your object as type Mother, but your class name is Mutter.

Your include guards are defective. In order for include guards to work correctly, they have to define the symbol being tested to prevent the contents from being included twice.
1
2
3
4
#ifndef MOTHER_H
#define MOTHER_H  // Causes the contents to be bypassed if included a second time
//  Contents of file
#endif 



Last edited on
I'm not exactly sure what the problem your describing is just because I'm not understanding what you're describing as the problem.

However, some issues I notice off the bat:

1. You aren't doing you're header guards correctly.

1
2
3
4
5
6
#ifndef MOTHER_H
#define MOTHER_H // You forgot to define it

// Class definition

#endif 


2. void setMother(Mutter mutterptr);

This is supposed to accept a pointer as it's argument, so make sure to make it a pointer: Mutter* mutterptr.

3. Learn to spell mother. :)
Thanks for your response.
You are right. I corrected the structure accordingly.
As it's a simple replica of my more complex structure,
the correction of the spelling, doesn't resolve the inheritance problem.
I hope my problem gets clear with the corrected version.



Line 11: You still have a capitalization issue.

Line 30: Capitalization issue again.

Line 41,54: The forward declaration for Child is unnecessary as Child must be fully declared before being used as a base class of Daughter or Son.

Line 32, 45, 58: The name implies a pointer, but as Tresky pointed out, your argument is not a pointer.

You can simplify your includes somewhat. mother.h does not need to include child.h, son.h or daughter.h. Only child is used in mother.h and since it's a pointer, the forward declaration is sufficient.

Likewise in child.h, the include for mother.h is not needed. The forward declaration for Child is all that is needed for a pointer to Child.

Also in daughter.h and son.h, mother.h is not needed but you do need a forward declaration for Mother and the argument to setMother needs to be a pointer.


Last edited on
Thanks, for your help.
The error was resolved by not just forward declaring the mother object in line 24 and not include the header of the mother.h in the base class.
I also tried to resolve the lasting typing errors in the example structure.
Thanks, again.
Topic archived. No new replies allowed.