what did i do wrong? Classes

first of all, to learn about classes i figured i would mess around with it see what i can and cannot do, and why things do and dont work.

i cant figure out why this is giving me numerous errors?

i didnt think you had to return integers from member functions of a class to main, do you?

my goal was to just display low on health with the member function (by temp decreasing it)

even in this small amount of code i get lost in my own code and cant figure out what the hell im doing lol

comments with test was just me trying to see how to do things and has no purpose in my problem of line 26

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace 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
Last edited on
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?)
Last edited on
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.
war_stat is suppose to be warrior_stat

is line 26 suppose to be? warrior_stat.warrior::damage();

in the change function, how do i change the variable?

and i dont know what you mean by
so use this-> or leave member names unqualified


Last edited on
http://cplusplus.com/doc/tutorial/classes/

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:

http://cplusplus.com/doc/tutorial/structures/

"Pointers to structures" (again, near the bottom of the page)
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?
Last edited on
so i just add the class and object in that function?
warrior warrior_stat;
ok i deleted the parameters of line 26 and added in beginning of function warrior::damage()
warrior warrior_stat;

so now when it compiles even if warrior_stat.damage is under 10 it still doesnt display
No, you're creating a warrior object inside of a member function of a warrior object.
damage() should be like
1
2
3
4
5
6
7
void warrior::damage()
{
    health -= 6;//test

    if(health < 10)//less than 10 display warning
        cout << "You are low on health";
}
ahh yes thank you naraku9333

so inside a member function of a class , you dont need to put a type for something refering to another part of the class?
Topic archived. No new replies allowed.