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

in sword class, sword function is working fine with this syntax but in gun class gun function is giving me this error : no matching function for call to weapon:weapon()

5 Answers5

3

A number of answers have already pointed out that you want to use a base initializer. You should also, however, initialize members the same way when possible, so your constructor should really look like this:

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

...and yes, it's entirely normal to do all the initialization in the initializer list, so the body of the ctor is empty (in fact, I generally prefer that).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Your constructor of gun should use initilaizer list to call the base constructor.

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

        capacity = c;
    }
Steephen
  • 14,645
  • 7
  • 40
  • 47
1

It's a simple typo. Change the gun constructor to

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

i.e. use a base class initialiser. You do this correctly in the sword class.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    I wouldn't call that a typo. *"Strike a mtach"* is a typo, the OP's code is more like *"strike a match and put it back in your pocket"*. – Beta May 18 '15 at 14:24
1

Compare the constructors for sword and gun. Specifically, gun's is as follows

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

It should be

gun(const string& n, int d, int c): weapon(n.d) {
    capacity = c;
}
kdopen
  • 8,032
  • 7
  • 44
  • 52
0

this works i use the protected specifier and a virtual so that you can display the info of both objects with the same function method

 #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;
}
Kryssel Tillada
  • 180
  • 3
  • 13