【问题标题】:C++ inheritance/polymorphism [duplicate]C ++继承/多态[重复]
【发布时间】:2015-07-30 02:05:38
【问题描述】:
    #include<iostream>

using namespace std;

class weapon {
private:
    int damage;
    string name;

public:
    weapon(const string& n, int d) {
        name = n;
        damage = d;
    }
};

class sword : public weapon {
private:
    int sharpness;
public:
    sword(const string& n, int d, int s) : weapon(n,d), sharpness(s) {}
};

class gun : public weapon {
private:
    int capacity;
public:
    gun(const string& n, int d, int c) {
        weapon(n,d);
        capacity = c;
    }
};

int main() {
    sword s("Katana", 72, 41);
    gun g("AK-47", 74, 30);
    return 0;
}

在剑类中,剑功能使用这种语法可以正常工作,但在枪类中,枪功能给我这个错误: 没有调用武器的匹配函数:weapon()

【问题讨论】:

  • 你为什么不像你的剑类那样构建你的枪类?你构建剑术的方式是正确的。
  • 提示:你的 sword 构造函数在做什么,而 gun 不是?
  • 猜猜没有人知道“复制”按钮在哪里......
  • 这不完全是重复的,更像是题外话>“由于简单的印刷错误造成的问题”。

标签: c++ inheritance polymorphism


【解决方案1】:

许多答案已经指出您要使用基本初始化程序。但是,您还应该尽可能以相同的方式初始化成员,因此您的构造函数应该看起来像这样:

gun(const string& n, int d, int c) : weapon(n, d), capacity(c) { }

...是的,在初始化列表中进行所有初始化是完全正常的,所以 ctor 的主体是空的(事实上,我通常更喜欢这样)。

【讨论】:

    【解决方案2】:

    gun 的构造函数应该使用初始化列表来调用基本构造函数。

    gun(const string& n, int d, int c) : weapon(n,d){
    
            capacity = c;
        }
    

    【讨论】:

      【解决方案3】:

      这是一个简单的错字。将枪构造函数更改为

      gun(const string& n, int d, int c) : weapon(n,d)
      {
          capacity = c;
      }
      

      即使用基类初始化器。您在 sword 类中正确执行此操作。

      【讨论】:

      • 我不会称之为错字。 “Strike a mtach” 是一个错字,OP 的代码更像是“打火柴然后放回你的口袋”
      【解决方案4】:

      比较swordgun 的构造函数。具体来说,gun的如下

      gun(const string& n, int d, int c) {
          weapon(n,d);
          capacity = c;
      }
      

      应该是

      gun(const string& n, int d, int c): weapon(n.d) {
          capacity = c;
      }
      

      【讨论】:

        【解决方案5】:

        这个工作我使用受保护的说明符和一个虚拟,这样你就可以用相同的函数方法显示两个对象的信息

         #include<iostream>
        
         using namespace std;
        
         class weapon {
         protected:
         int damage;
         string name;
        
        public:
        weapon(const string& n, int d) {
            name = n;
            damage = d;
        }   
        void show_wep();
        };
        
        void weapon::show_wep()
        {
        cout << "damage: " << damage << endl
             << "name: " << name << endl;
        }
        
        class sword : public weapon {
        protected:
        int sharpness;
        public:
        sword(const string& n, int d, int s) : weapon(n,d), sharpness(s) {}
        virtual void show_wep();
        };
        
        void sword::show_wep()
        {
        cout << "damage: " << damage << endl
             << "name: " << name << endl
             << "sharpness: " << sharpness << endl;
        }
        
        class gun : public weapon {
        protected:
        int capacity;
        public:
        gun(const string& n, int d, int c) : weapon(n,d), capacity(c) {}
        virtual void show_wep();
        };
        
        void gun::show_wep()
        {
        cout << "damage: " << damage << endl
             << "name: " << name << endl
             << "capacity: " << capacity << endl;
        }
        
        int main() {
        sword s("Katana", 72, 41);
        gun g("AK-47", 74, 30);
        
        s.show_wep();
        cout << endl;
        g.show_wep();
        
        
        return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2013-10-24
          • 1970-01-01
          • 2016-08-09
          • 1970-01-01
          • 2012-04-11
          • 1970-01-01
          • 2020-06-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多