scope + pointer problem

i get an "error : swordpick was not declared in this scope" for the line:

enemy->TakeDamage(SwordPick->GetDamage()* SwordPick->GetQuality()/10);

it's towards the end of the program. what should the correct syntax be?

the whole program is given below.



#include <iostream>
using namespace std;

class Sword {
private:
int damage; //max damage
int quality; //sharpness of sword
public:
Sword(){
damage=20;
quality=10;
}

int GetDamage(){
return damage;
}

int GetQuality(){
return quality;
}

void SetQuality(int setquality){
quality=setquality;
}
};



class Player {
private:
int health;
int weapon;

public:
Player() {
health=100;//1-100
weapon=0;//0-3
}

void TakeDamage(int dmg) {
health -= dmg;
}

void output_health() {
cout << health << endl;
}

// weapon pickup
void PickupSword(Sword* SwordPick) {weapon=1;}

//void PickupBowarrow(Bowarrow* BowarrowPick) {weapon=2;}

//void PickupAxe(Axe* AxePick) {weapon=3;}


void attack(Player* enemy){
if (weapon=1) {
enemy->TakeDamage(SwordPick->GetDamage()* SwordPick->GetQuality()/10);
SwordPick->SetQuality(GetQuality()-1);
cout << "enemy health = " << enemy->output_health << endl;
cout << "weapon quality = " << SwordPick->GetQuality << endl;
}
//if (weapon=2) {enemy->TakeDamage(BowarrowPick->GetDamage()* BowarrowPick->GetQuality()/10;}
//if (weapon=3) {enemy->TakeDamage(AxePick->GetDamage()* AxePick->GetQuality()/10;}
//if (weapon=0) {cout << "does not have a weapon" << endl;}
}
};

int main(){
Player Goblin, Elf;
Sword Greysword;

Elf.PickupSword(&Greysword);
Elf.attack(&Goblin);

return 0;
}
I don't see where SwordPick is defined.
so does this mean that this is completely the wrong way of going about it? and that it's not just about something being out of scope?
Edit: I stand corrected on the first part of my answer. I was lazy and didn't check the indenting.

As for SwordPick, that's an integer. How is the compiler supposed to call the GetDamage class function on an integer? Oh wait, it's not an integer. It's a double? A string? Who knows. You never created any object named SwordPick.
Last edited on
That's not the whole definition of the player class:

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
class Player {
     private:
          int health;
          int weapon;

     public:
          Player() {
               health=100;//1-100
               weapon=0;//0-3
          }

          void TakeDamage(int dmg) {
               health -= dmg;
          }

          void output_health() {
               cout << health << endl;
          }

          // weapon pickup
          void PickupSword(Sword* SwordPick) {weapon=1;}

          //void PickupBowarrow(Bowarrow* BowarrowPick) {weapon=2;}

          //void PickupAxe(Axe* AxePick) {weapon=3;}


          void attack(Player* enemy){
               if (weapon=1) {
                    enemy->TakeDamage(SwordPick->GetDamage()* SwordPick->GetQuality()/10);
                    SwordPick->SetQuality(GetQuality()-1);
                    cout << "enemy health = " << enemy->output_health << endl;
                    cout << "weapon quality = " << SwordPick->GetQuality << endl;
               }

               //if (weapon=2) {enemy->TakeDamage(BowarrowPick->GetDamage()* BowarrowPick->GetQuality()/10;}
               //if (weapon=3) {enemy->TakeDamage(AxePick->GetDamage()* AxePick->GetQuality()/10;}
               //if (weapon=0) {cout << "does not have a weapon" << endl;}
          }
};


On that note, jay75 please indent your code in the future.

The problem is SwordPick is being passed in to PickupSword but it is never being assigned after that. There is also some back-and-forth here where you have a Sword pointer being passed in but only using an int to identify the weapon. Consider giving the player a Sword* as a member (or, better yet, use inheritance/polymorphism and give the player a Weapon* as a member and have Sword, Bowarrow, and Axe inherit from Weapon). This would save you from using ints or enums to check which weapon they have.
Last edited on
thanks shaktar, i'll try the weapon route.
make sure you destruct the constructors
Topic archived. No new replies allowed.