【发布时间】: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