Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to inherit constructors? OR how to make similar constructors?

I am making a class for a character with several attributes. I made it so the user has to choose between 3 objects made from the constructor of that first class.
I cant think of a way to choose between the objects so I want to create a class that inherits the attributes of the first class(basically a copycat) but will only copy the chosen object.

#include <iostream>
#include <cmath>
#include <windows.h>

using namespace std;

class Character {
    public:
        string weapon;
        float HP;
        float MP;
        float str;
        float def;

        Character(string aweapon, float aHP, float aMP, float astr, float adef){
            weapon = aweapon;
            HP = aHP;
            MP = aMP;
            str = astr;
            def = adef;
        }
};

class Chose : public Character{
    
};

int main()
{

    Character warrior("sword", 100, 20, 50, 50);

    Character tank("shield", 200, 20, 25, 80);

    Character magician("staff", 80, 100, 30, 30);

    Chose that; // error is here

    cout << warrior.HP << endl;

    return 0;
}

error says:-

|24|error: no matching function for call to 'Character::Character()'
|15|candidates are:
|15|note: Character::Character(std::string, float, float, float, float)
|15|note:   candidate expects 5 arguments, 0 provided
|7|note: Character::Character(const Character&)
|7|note:   candidate expects 1 argument, 0 provided
|39|note: synthesized method 'Chose::Chose()' first required here 

Sooooo, I can’t figure out the problem here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

class Chose : public Character{
public:
    using Character::Character;
};

https://godbolt.org/z/TEG7Pvdxa

Note that you are trying use default constructor of Character which was removed since custom constructor was defined. As result default constructor of Chose can’t be created implicitly (since base class do not have default constructor).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading