#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int change(int);
class warrior
{
public:
int health;
int speed;
void attack();
void damage();
};
int main()
{
warrior warrior_stat;
warrior_stat.health = 15;
cout << warrior_stat.health <<endl;//test
cout << change(warrior_stat.health);//test
warrior_stat.damage(war_stat.health);
}
int change(int i)//test
{
return i*2;
}
void warrior::damage()
{
warrior_stat.health -= 6;//test
if(warrior_stat.health < 10)//less than 10 display warning
cout << "You are low on health";
}
line 26 i was trying to call warrior::damage() to decrease health and display the warning, i know i screwed up the call(should be warrior::damage(), but still doesnt work either way
errors are ;line 26
and line 36 which says the member variable not in scope
I'm no expert, but you are trying to pass war_stat.health to a function that accepts no parameters/ arguments. Is this supposed to work without function overloading or am I missing something?
Edit:
The above was a really quick skim of your code, after looking a it again:
What is war_stat? Should it not be warrior_stat?
warrior_stat is not defined in your damage function (something I haven't covered yet maybe?)
Your change() function doesn't actually change the variable you pass to it. I can't tell if that is your intent or not.
The warrior::damage() method is trying to modify warrior_stat, an instance of warrior completely unrelated. You probably want to change the health of the warrior who is calling the method, so use this-> or leave member names unqualified.
Near the bottom of the page, "Pointers to classes" (for the -> operator). You can also check out data structures as I think it's mentioned in there somewhere:
No. Line 26 is fine, but you can't pass a parameter.
In change(), you can try passing by reference if you want to modify the parameter.
Look at just lines 34-40. Inside this method, you're trying to access something called warrior_stat. That doesn't exist anywhere inside that function. What are you trying to modify?