Need help!

I need to have attack skill be used for the Sword and Nunchuck attack function that is overriding the weapon virtual function. I can't figure it out. The equations for the attack damage is the ones shown for each classes' attack function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <string>

using namespace std;

class Weapon {
private:
    string name;
    int attackDamage;
    int attackDecay;
public:
    Weapon() {
        this->name = "";
        this->attackDamage = 0;
        this->attackDecay = 0;
    }
    Weapon(string name, int attackDamage, int attackDecay) {
        this->name = name;
        this->attackDamage = attackDamage;
        this->attackDecay = attackDecay;
    }

    virtual int attack(int attackSkill) {
        int damage;
        damage = attackDamage;
        attackDamage = (attackDamage + attackSkill) - attackDecay;
        return damage;
    }
   
    int getAttackDamage() {
        return this->attackDamage;
    }
    int getAttackDecay() {
        return this->attackDecay;
    }
    string getName() {
        return this->name;
    }

    void setAttackDamage(int newAttackDamage) {
        this->attackDamage = newAttackDamage;
    }
    void setAttackDecay(int newAttackDecay) {
        this->attackDecay = newAttackDecay;
    }
    void setName(string newName) {
        this->name = newName;
    }
};

class Sword : public Weapon {
private:
    int attackDamage;
    int attackDecay;
public:
    Sword() {
        this->attackDamage = 0;
        this->attackDecay = 0;
    }
    Sword(int attackDamage, int attackDecay) {
        Weapon("Sword", attackDamage, attackDecay);
    }


    int getAttackDamage() {
        return this->attackDamage;
    }
    void setAttackDamage(int newAttackDamage) {
        this->attackDamage = newAttackDamage;
    }
    int getAttackDecay() {
        return this->attackDecay;
    }
    void setAttackDecay(int newAttackDecay) {
        this->attackDecay = newAttackDecay;
    }
    int attack() {
        int damage;
        damage = attackDamage;
        attackDamage = (attackDamage + attackSkill) - attackDecay;
        return damage;
    }
};

class Nunchucks : public Weapon {
private:
    int attackDamage;
public:
    Nunchucks() {
        Weapon("Nunchucks", 0, 0);
    }
    Nunchucks(int attackDamage) {
        Weapon("Nunchucks", attackDamage, 0);
    }

    int getAttackDamage() {
        return this->attackDamage;
    }
    void setAttackDamage(int newAttackDamage) {
        this->attackDamage = newAttackDamage;
    }
    int attack() {
        int damage;
        damage = attackDamage;
        attackDamage = (attackSkill * attackDamage) / 10;
        return damage;
    }
};

class Ninja {
private:
    string name;
    int health;
    int attackSkill;
protected:
    Weapon* weapon = new Sword;
public:
    Ninja() {
        this->name = "";
        this->health = 0;
        this->attackSkill = 0;
        this->weapon = nullptr;
    }
    Ninja(string name, int health, int attackSkill) {
        this->name = name;
        this->health = health;
        this->attackSkill = attackSkill;
        this->weapon = nullptr;
    }
    Ninja(Ninja& player) {
        cout << "Copy Constructor called" << endl;
        this->name = player.getName();
        this->health = player.getHealth();
        this->attackSkill = player.getAttackSkill();
        //Need to create a New Sword
        Weapon* weaponCopy;
        weaponCopy = new Weapon("", 0, 0);
        weaponCopy->setAttackDamage(player.getSword()->getAttackDamage());
        weaponCopy->setAttackDecay(player.getSword()->getAttackDecay());
        this->weapon = weaponCopy;
    }
    Ninja& operator=(Ninja& player) {
        cout << "Overload Operator Called" << endl;
        this->name = player.getName();
        this->health = player.getHealth();
        this->attackSkill = player.getAttackSkill();
        Sword* swordOverload;
        swordOverload = new Sword(0, 0);
        swordOverload->setAttackDamage(player.getSword()->getAttackDamage());
        swordOverload->setAttackDecay(player.getSword()->getAttackDecay());
        this->weapon = swordOverload;
    }

    void setName(string newName) {
        this->name = newName;
    }
    string getName() {
        return this->name;
    }
    void setHealth(int newHealth) {
        this->health = newHealth;
    }
    int getHealth() {
        return this->health;
    }
    void setAttackSkill(int newAttackSkill) {
        this->attackSkill = newAttackSkill;
    }
    int getAttackSkill() {
        return this->attackSkill;
    }
    Weapon* getSword() {
        return this->weapon;
    }
    void pickupWeapon(Weapon* weapon) {
        this->weapon = weapon;
    }
    int attack() {
        int damage;
      
        damage = weapon->attack(attackSkill) + attackSkill;
       
        return damage;
    }
    
};
When compiling it has the following errors/warning:
 In member function 'int Sword::attack()':
80:40: error: 'attackSkill' was not declared in this scope
 In member function 'int Nunchucks::attack()':
105:25: error: 'attackSkill' was not declared in this scope
 In member function 'Ninja& Ninja::operator=(Ninja&)':
152:5: warning: no return statement in function returning non-void [-Wreturn-type]


You don't have a member variable attackSkill.

In class Sword you have:
1
2
int attackDamage;
    int attackDecay;
and in class Nunchucks
int attackDamage;
They are hiding the variables of class Weapon. They shouldn't there.

This
1
2
3
    Nunchucks() {
        Weapon("Nunchucks", 0, 0); // Creates and dismisses a temporary variable
    }
should be this:
1
2
    Nunchucks() : Weapon("Nunchucks", 0, 0) { // Calls the base constructor
    }
Topic archived. No new replies allowed.