Question

I seem to be having a problem with Inheritance. Whenever I try to override a function in my Character.cpp file I get an error stating:'No default constructor exists for class Character'. I'm fairly new to c++ and I don't get why the error was appearing. What I want to do is basically override the GetFierceAttack() of the default Character class to be stronger for the child class which is called Ogre. I was wondering if I was using 'virtual' correctly.

My Character.h file

#ifndef CHARACTER_H
#define CHARACTER_H
#include <time.h>
#include <cstdlib>

class Character
{
protected:
    int Health, LightAttack, FierceAttack, Damage;

public:
    Character(int health, int lAttack, int fAttack, int damage);
    ~Character();

public:
    int GetHealth()                     {return Health;}
    int GetLightAttack()                {return (rand()%5)+10;}
    virtual int GetFierceAttack()       {return (rand()%5)+10;}

    void DeductDamage(int Damage);  
};

//Overrides Fierce Attack
class Ogre:public Character
{
public:
    Ogre(int health, int lAttack, int fAttack, int damage);

    int GetFierceAttack()           {return (rand()%10)+20;}
};

#endif

My Chacter.cpp file

#include "Character.h"

//Constructor
Character::Character(int health, int lAttack, int fAttack, int damage)
{
    Health = health;
    LightAttack = lAttack;
    FierceAttack = fAttack;
    Damage;
}

//Destructor
Character::~Character()
{
}

void Character::DeductDamage(int Damage)
{
    Health -= Damage;

    if(Health < 0)
    {
        Health = 0;
    }
}

//Constructor
Ogre::Ogre(int health, int lAttack, int fAttack, int damage) //Here's the error
{
}

//Destructor
Ogre::~Ogre()
    {
    }
Was it helpful?

Solution

When you define constructor for class by yourself, you are overriding the default implementation. So you need to provide default constructor as

          class Character
           {
               public:
               Character(){}
               //----------your code-----//
            }

          Similarly for your destructor problem you first decalre destructor in class and then define it in .cpp file.

          class Ogre:public Character
           {
              public:
               ~Ogre();
           }

OTHER TIPS

When you declare a non-default constructor for a class, the compiler does not generate a default constructor anymore.

So you have to provide your own.

When you call the constructor on a derived class, what happens is this:

  1. the base object gets constructed first
  2. any variables in the initialisation list get initialised
  3. the body of the (derived) constructor is executed

If you do not specify a different constructor, the default constructor will be used for (1). You are getting a compiler error because you have no default constructor.

To specify a different constructor, do something like this:

Ogre::Ogre(int health, int lAttack, int fAttack, int damage) : Character (health, lAttack, fAttack, damage)
{
    // body of constructor
}

Then the instance of the constructor that matches those parameters will be called instead. Since you have defined that, you won't get an error.

As for the error on the destructors, firstly, as others have said, you can't override the destructor without declaring it in the class. So you could add ~Ogre() to your class definition. However, you're not currently doing anything in your destructor body, so why are you redefining it at all? Just leave it as the default destructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top