Inhertiance error help

I am getting this error in my subclass of Monster. What is going on how can I fix it so the error doesn't show up. It says No matching function from call to Enemy::Enemy()
Monster::Monster(int,int) already defined here.

Monster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Monster.h"

Monster::Monster(int EnemyHealth,int EnemyMana)
{
    //ctor
}


int Monster::dropxp(int enemydropxp){


}

Monster.h
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
#ifndef MONSTER_H
#define MONSTER_H
#include "Enemy.h"



class Monster : public Enemy
{
     public:
         Monster() {};
         Monster(int EnemyHealth,int EnemyMana):
             Enemy(int EnemyHealth,int EnemyMana);
    void TheenemyHealth()
    {
        int Enemyhealth = 100;
    }
    int EnemyDamage(int EnemyAttack){
    int Attack = EnemyAttack;
    Attack = 5;
    }
    int dropxp(int enemyxpdrop);
    private:

};

#endif // MONSTER_H 

Enemy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef ENEMY_H
#define ENEMY_H


 class Enemy
{
public:
    Enemy(int EnemyHealth,int EnemyMana);
    ~Enemy();
      virtual void TheenemyHealth(){}
     virtual int EnemyDamage(int EnemyAttack){
int  Attack = EnemyAttack;
     }
virtual int dropxp(int enemyxpdrop);



private:
  int Attackpower= 0;
  int Strenght = 0;
  int Enemyxp= 0;

};

Enemy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Enemy.h"

Enemy::Enemy(int EnemyHealth,int EnemyMana)
{
    Attackpower;
    Strenght;
    Enemyxp;

}

Enemy::~Enemy()
{
    //dtor
}
Blackhart98 wrote:
11
12
         Monster(int EnemyHealth,int EnemyMana):
             Enemy(int EnemyHealth,int EnemyMana);
This is nonsensical. What is this even supposed to mean?
Last edited on
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
class Enemy
{
    public:
        Enemy() = default ; // explicitly defaulted

        // see: http://en.cppreference.com/w/cpp/language/initializer_list
        Enemy( int EnemyHealth, int EnemyMana ) : health(EnemyHealth), mana(EnemyMana) {}

    // ...

    private:
        int health = 0 ;
        int mana = 0 ;
};

class Monster : public Enemy
{
    public:
        // http://www.stroustrup.com/C++11FAQ.html#inheriting
        using Enemy::Enemy ; // inheriting constructor

        // ...

    private:
        int whatever = 0 ; // note: has in-class member initialiser
};

class Monster2 : public Enemy
{
    public:
        Monster2() : whatever(0) {} // augmented by the implementation
                                    // to default-construct the base class object

        Monster2( int EnemyHealth, int EnemyMana )
            : Enemy(EnemyHealth,EnemyMana), // initialise base class object
              whatever(0) {} // initialise member object
        // ...

    private:
        int whatever ;
};
@LB how please tell me I am still trying to figure this mess out.
Blackhart98 wrote:
@LB how please tell me I am still trying to figure this mess out.
You want me to say: You are still trying to figure this mess out.

Jokes aside, please either answer my question or ask a question about JLBorges' code.
Well I got this code from stackoverflow.

1
2
Monster(int EnemyHealth,int EnemyMana):
             Enemy(int EnemyHealth,int EnemyMana);

What do you think should be here for the inhertiance to work? Like I said the errors keeps coming about I just need to know the right structure to put the Monster class in.
You can't write the initializer list in the decleration, you have to make it in the implementation.
Monster(int EnemyHealth,int EnemyMana); // decleration
1
2
3
Monster::Monster(int EnemyHealth,int EnemyMana) // implementation
 : Enemy(int EnemyHealth,int EnemyMana) 
{}


But you could just write it in the header file as well if you want:
1
2
3
4
5
6
7
8
class Monster : public Enemy
{
public:
    Monster(int EnemyHealth,int EnemyMana)
     : Enemy(EnemyHealth, EnemyMana) 
    {}
    // ...
}; 
Last edited on
This is wrong:
1
2
3
Monster::Monster(int EnemyHealth,int EnemyMana) // implementation
 : Enemy(int EnemyHealth,int EnemyMana) 
{}
This is right:
1
2
3
Monster::Monster(int EnemyHealth,int EnemyMana) // implementation
 : Enemy(EnemyHealth, EnemyMana) 
{}
You do not specify types when passing parameters.
Oh yeah, I just copied the stuff above and didn't look, my bad
Thanks it work
Last edited on
Topic archived. No new replies allowed.