【问题标题】:How to inherit constructors? OR how to make similar constructors?如何继承构造函数?或者如何制作类似的构造函数?
【发布时间】:2022-07-25 05:20:31
【问题描述】:

我正在为具有多个属性的角色创建一个类。我做到了,因此用户必须在由第一类的构造函数制成的 3 个对象之间进行选择。 我想不出一种在对象之间进行选择的方法,所以我想创建一个继承第一个类的属性的类(基本上是一个模仿者),但只会复制所选对象。

#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;
}

错误说:-

|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,我无法解决这里的问题。

【问题讨论】:

    标签: c++ class object inheritance constructor


    【解决方案1】:
    class Chose : public Character{
    public:
        using Character::Character;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 2023-03-07
      • 1970-01-01
      • 2021-08-13
      • 2012-09-03
      • 2013-06-01
      相关资源
      最近更新 更多