not declared in this scope

okay, so, generally, I know what that means, that I didn't declare the variable, but I'm using a bunch of classes, and I'm making a variable IN a constructor, I try to use it in a different function, and I get that error, that hp is not declared in this scope. If needed I will paste other files in replies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Fighter.h"
#include <iostream>
#include <string>
#include "main.h"
#include "Enemy.h"
#include "self.h"

Fighter::Fighter()
{
    hp = 50;
    Fighter Fi;
}

void getHurt()
{
    hp -= 15;
}

Fighter::~Fighter()
{
    //dtor
}
In this case you will want your 'getHurt' function to be included inside your fighter class so that it can easily access all of the variables.

So change it to
void Fighter::getHurt()
and add it in your header file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Fighter
{
private: 
  int hp;
public:
  Fighter();
  void getHurt();
};

Fighter::Fighter()
{
    hp = 50;
    Fighter Fi;
}

void Fighter::getHurt()
{
    hp -= 15;
}
Last edited on
Alright, so, now it runs, and stops working immediately. Which, honestly isn't much of a loss of progress, all I had actualyl RUNNING was a loop of 2 fighterrs attacking the player. Should I paste all my files?
Topic archived. No new replies allowed.