How come I can not use class initialization to initalize inherited attributes?

It keeps saying "Class Troll does not have any field named Health"
But, I inherited BaseEnemy within Troll which DOES have a short Health and a short AttackMag.
The class Troll does not have it's own Health but BaseEnemy does and BaseEnemy is the base of Troll.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include "Player.h"
#include "BaseEnemy.h"
#include "Troll.h"

using namespace std;



Troll::Troll( ):Health( 100 ),AttackMag( 10 )
{
    //ctor
}

void Troll::Attack(Player &Target)
{
    Target.Health -= this->AttackMag;
    if(Target.Health<0)
        Target.Health=0;
}
Void Troll::SpecialAttack(Player &Target)
{
    Target.Health -= (this->AttackMag * 3)
}


Whats going on?
Last edited on
Are you using public inheritance?
Hi Helios. yes I am. I can post the code if you want. Here.

troll.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef TROLL_H
#define TROLL_H

class Troll : public BaseEnemy
{
    public:
        Troll();
        void Attack(Player &);
        void SpecialAttack (Player &);

};

#endif // TROLL_H 


BaseEnemy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Player.h"
#include "BaseEnemy.h"
#include "Troll.h"

using namespace std;


void BaseEnemy::Attack(Player &Target)
{
    Target.Health -= this->AttackMag;
}


BaseEnemy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef BASEENEMY_H
#define BASEENEMY_H

class Player;
class BaseEnemy
{
    public:
        BaseEnemy();
        short Health;
        short AttackMag;
        virtual void Attack(Player &);

};

#endif // BASEENEMY_H 


And the troll.h is the in the Original Post. I dont know why it doesnt work
Last edited on
*bump*

I still can't figure this out. Can someone help me?
I've tried

Troll::Troll(): BaseEnemy (Health)

but this doesn't work either
Try getting rid of the public in front of baseEnemy. See if that works
Hi Frantz. I haven't tried that yet but I don't think that would be the problem.

But, I found a solution to my problem!

If anyone's interested, here's the renewed source codes! It took me a few hours to solve this
awful problem.

Troll.cpp
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
#include <iostream>
#include "Player.h"
#include "BaseEnemy.h"
#include "Troll.h"

using namespace std;



Troll::Troll(short a, short b): BaseEnemy(a,b)
{

    cout<< Health;
}

void Troll::Attack(Player &Target)
{
    Target.Health -= this->AttackMag;
    if(Target.Health<0)
        Target.Health=0;
}
void Troll::SpecialAttack(Player &Target)
{
    Target.Health -= (this->AttackMag * 3);
}


Troll.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef TROLL_H
#define TROLL_H

class Troll : public BaseEnemy
{
    public:
        Troll(short a, short b);
        void Attack(Player &);
        void SpecialAttack (Player &);

};

#endif // TROLL_H 


BaseEnemy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Player.h"
#include "BaseEnemy.h"
#include "Troll.h"

using namespace std;

BaseEnemy::BaseEnemy(short H, short A)
: Health(H), AttackMag(A)
{

}

void BaseEnemy::Attack(Player &Target)
{
    Target.Health -= this->AttackMag;
}


BaseEnemy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef BASEENEMY_H
#define BASEENEMY_H

class Player;
class BaseEnemy
{
    public:
        BaseEnemy(short h, short a);
        short Health;
        short AttackMag;
        virtual void Attack(Player &);

};

#endif // BASEENEMY_H 


The reason why it took me so long to solve this was because I never knew about the "calling the base constructor" thing. I researched a lot and finally I stumbled across it after a few hours.

I'm proud to say it works!
Topic archived. No new replies allowed.