not sure if i'm on the right track for my "use" method for game

Okay, working on a project and could use some advice, not looking for you to write my code, so the way my Prof. explained the the getters and setters i believe i have all that is needed for the variables but what really has me stumped is the "use" method, i know that it is referencing the user whether it be class enemy or player but i'm not sure where to go from there, i'm still trying to understand and incorporate pointers and referencing

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
#include "Item.h"
#include <string>

using namespace std;

int Item::getweight()
{
        return m_nweight;
}

void Item::setweight(int nweight)
{
        m_nweight = nweight;
}

int Item::getvalue()
{
        return m_nvalue;
}

void Item::setvalue(int nvalue)
{
        m_nvalue = nvalue;
}

int Item::getquantity()
{
        return m_nquantity;
}

void Item::setquantity(int nquantity)
{
        m_nquantity = nquantity;
}

void use(Unit & user)
{
        nquantity--;
        nvalue--;
        nweight--;
       
}
Last edited on
you have a problem in your use function:
it is of type void but returns?
but I didn't understood what was your problem?
No, it should be void, i just didnt realize i put a return statement in there. my problem is: shouldn't my use method interact with the user? or do i not need to reference the user in the use method. "use' should subtract the quantity, value and weight from the user
I would say you don't need the user because it looks ike the quanitty , value , and weight are part of the item class not user class. The items are probably in the user class so when you use the item the player has it will modify the item directly. I would probably check to see if you have the item ( quantity > 0 ) before allowing you to use something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool use( void )
{
    bool used = false;

    if( nquantity > 0 )
    {
        --nquantity;
        --nvalue;
        --nweight;
        used = true;
    }

    return used; //was the item used?
}
Okay, so I was right with what I wrote, I guess I was just over thinking it. Thank you
Topic archived. No new replies allowed.