I have simple problem:
class Weapon{
public:
int dmg;
float speed;
int rate;
};
class Uzi : public Weapon{
public:
Uzi();
void Shot(float angle);
};
Class Pistol : public Weapon{
public:
Pistol();
void Shot(float angle);
};
Later in code when I reserve for example:
Weapon wep;
wep = Uzi;
wep.Shot(15);
It doesn’t work:
undefined reference to `Weapon::Shot(float)’
Can I reserve different type of ‘wep’ variable?
I think no because weapons are changing(pistol/uzi/…).
Thanks in advance!
This is slicing.
Uziis-aWeapon, but not the other way around. You can use pointers for this:Also, you get the error because there is no
Shot()method in theWeapon()class.You might want to declare it
virtualand also make it abstract (optional). You make itvirtualto allow polymorphism.That way:
will call
Shot()in theUziclass, although it’s called on aWeaponpointer.The following should work: