Page 1 of 1

N00bish function wont work with class thingy

Posted: Mon May 04, 2009 12:26 pm
by Zer0XoL
Ok, hello :)
I thought i would make a simple text game before continuing with SDL
And well, i have problems xD.
Now, everytime i try to attack Slagor hes HP is always "reset"(not good), BUT i can decrese his HP in the main function.(but i want it to work in the attack function)
And here is the WHOLE code:) :

Code: Select all

#include <iostream>
using namespace std;
class Player
{
      private:
              bool IsAlive;
              int exp, hp, mp, power;
      public:
             char name;
             int GetHP()
             {
                   return hp;
                   }
              void AddHP(int h)
              {
                   hp += h;
                   }
              
              
              void Die(){
                   IsAlive = false;
                   }
              void Attack(Player e)
              {
                   e.SubHP(exp*power);
                   cout << "You attacked " << "another player" << " with " << exp*power << " damage!\n";
                   cout << "Enemy hp left: " << e.GetHP() << "!\n";
                   }
                   
              void Default()
              {
                   IsAlive = true;
                   exp = 1;
                   hp = 100;
                   power = 1;
                   mp = 50;
                   }
              void SubHP(int p)
              {
                   hp = hp - p;
                   }
                   
                   
                   
};
int main()
{   
    cout << "Welcome!\n\n\n";
    Player Zer0XoL;
    Zer0XoL.Default();
    Player Slagor;
    Slagor.Default();
    Zer0XoL.Attack(Slagor);
    Zer0XoL.Attack(Slagor);
    Zer0XoL.Attack(Slagor);
    Zer0XoL.Attack(Slagor);
    Slagor.Attack(Zer0XoL);
    Zer0XoL.Attack(Slagor);
    Zer0XoL.Attack(Slagor);
    Slagor.SubHP(66);
    cout << "New hp:" << Slagor.GetHP() <<endl;
    Zer0XoL.Attack(Slagor);
    Zer0XoL.Attack(Slagor);
    Zer0XoL.Attack(Slagor);
    Slagor.SubHP(-1);
    cout << "New hp: " << Slagor.GetHP() <<endl;
    Zer0XoL.Attack(Slagor);
    Zer0XoL.Attack(Slagor);
    Zer0XoL.Attack(Slagor);
    cin.get();
    cin.get();
}
If you have thoughts about that i do something wrong in anyway, just tell me and ill learn more, thank you. :lol:

*Edit:
this is the output of the program(im using Dev-Cpp)
:

Code: Select all

Welcome!


You attacked another player with 1 damage!
Enemy hp left: 99!
You attacked another player with 1 damage!
Enemy hp left: 99!
You attacked another player with 1 damage!
Enemy hp left: 99!
You attacked another player with 1 damage!
Enemy hp left: 99!
You attacked another player with 1 damage!
Enemy hp left: 99!
You attacked another player with 1 damage!
Enemy hp left: 99!
You attacked another player with 1 damage!
Enemy hp left: 99!
New hp:34
You attacked another player with 1 damage!
Enemy hp left: 33!
You attacked another player with 1 damage!
Enemy hp left: 33!
You attacked another player with 1 damage!
Enemy hp left: 33!
New hp: 35
You attacked another player with 1 damage!
Enemy hp left: 34!
You attacked another player with 1 damage!
Enemy hp left: 34!
You attacked another player with 1 damage!
Enemy hp left: 34!
As you can see the hp always "resets". =/

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 12:54 pm
by dani93
When you call the function

Code: Select all

void Attack(Player e)
the object form main is copied to the object called "e". "e" is destroyed after the execution leaves the scope. So you don't change the correct object. To use the object from main, you can pass a reference (or a pointer) to it.

Code: Select all

void Attack(Player& e)
Do you understand what I mean?

There are some other things in your code that aren't... I mean they are not wrong... but also not nice ;)
For the function "Default" you could use a constructor. The definition of the class and the implementations of the methods should be in seperate files.

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 1:04 pm
by Zer0XoL
dani93 wrote:When you call the function

Code: Select all

void Attack(Player e)
the object form main is copied to the object called "e". "e" is destroyed after the execution leaves the scope. So you don't change the correct object. To use the object from main, you can pass a reference (or a pointer) to it.

Code: Select all

void Attack(Player& e)
Do you understand what I mean?

There are some other things in your code that aren't... I mean they are not wrong... but also not nice ;)
For the function "Default" you could use a constructor. The definition of the class and the implementations of the methods should be in seperate files.
Thank you, i couldn´t define the variables in the class so i made a function.. how does constructors work, or what are they?(im reading a swedish c++ book, i SHOULD know :shock: )
Also im planning to splitt it into files, but not yet. ;)
and yes i understand what you mean with pointers but i didnt rely try it.
Thank you, a lot :) :worship:

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 1:05 pm
by Ewan
In your Attack function, it's a copy of the instance of Player that's being passed as an argument, not the actual instance. It's like copying a file and putting it into a different folder then modifying the contents of the new file, when really you want to change the data in the original.

You'll need to do this instead:

Change your Attack function to

Code: Select all

     
void Attack(Player *e)
{
       e->SubHP(exp*power);
       cout << "You attacked " << "another player" << " with " << exp*power << " damage!\n";
       cout << "Enemy hp left: " << e->GetHP() << "!\n";
}
and every time you use Attack you will need to do it like this:

Code: Select all

Zer0XoL.Attack(&Slagor);
http://www.cplusplus.com/doc/tutorial/pointers/

Hope this helped :)

Ewan

EDIT: Hehe you beat me to it :mrgreen:

EDIT 2: Dani93's solution is better lol, use his :)

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 1:08 pm
by Zer0XoL
Ewan wrote:In your Attack function, it's a copy of the instance of Player that's being passed as an argument, not the actual instance. It's like copying a file and putting it into a different folder then modifying the contents of the new file, when really you want to change the data in the original.

You'll need to do this instead:

Change your Attack function to

Code: Select all

     
void Attack(Player *e)
{
       e->SubHP(exp*power);
       cout << "You attacked " << "another player" << " with " << exp*power << " damage!\n";
       cout << "Enemy hp left: " << e->GetHP() << "!\n";
}
and every time you use Attack you will need to do it like this:

Code: Select all

Zer0XoL.Attack(&Slagor);
http://www.cplusplus.com/doc/tutorial/pointers/

Hope this helped :)

Ewan

EDIT: Hehe you beat me to it :mrgreen:

EDIT 2: Dani93's solution is better lol, use his :)
Hey thanks, this is a rely great forum (thumbs up) :)

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 1:12 pm
by dani93
Zer0XoL wrote:how does constructors work, or what are they?
A constructor is automatically called when you create an object. It has the same name as the class.
Your constructor could look like that:

Code: Select all

Player ()                // same name as class
{

                   IsAlive = true;
                   exp = 1;
                   hp = 100;
                   power = 1;
                   mp = 50;

}
So you don't need to call "Default" everytime you create an object.
Ewan wrote: You'll need to do this instead:

Change your Attack function to
[...]
Yes, that is the pointer-version ;)
But I like the comparism to files.
Ewan wrote:EDIT: Hehe you beat me to it :mrgreen:
:)
Ewan wrote:EDIT 2: Dani93's solution is better lol, use his :)
I'm not really a C++ pro. Why should a reference be better than a pointer? OK, you don't need to care about if you use values or adresses ;)

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 1:32 pm
by Zer0XoL
dani93 wrote:
Zer0XoL wrote:how does constructors work, or what are they?
A constructor is automatically called when you create an object. It has the same name as the class.
Your constructor could look like that:

Code: Select all

Player ()                // same name as class
{

                   IsAlive = true;
                   exp = 1;
                   hp = 100;
                   power = 1;
                   mp = 50;

}
So you don't need to call "Default" everytime you create an object.

I'm not really a C++ pro. Why should a reference be better than a pointer? OK, you don't need to care about if you use values or adresses ;)

Ooh, that is really usefull feature! :shock2:
I can´t thank you more :) :worship:

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 1:47 pm
by Ewan
dani93 wrote:
Ewan wrote:EDIT 2: Dani93's solution is better lol, use his :)
I'm not really a C++ pro. Why should a reference be better than a pointer? OK, you don't need to care about if you use values or adresses ;)
Well, mainly because with pointers you have to use -> to access member functions, and when passing the argument you have to put &Slagor or whatever instead of just Slagor. Not that references are always better, but this time they just made more sense. :mrgreen:

Btw, when I said "do this instead" I was saying instead of his original code - I hadn't seen your reply.

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 1:53 pm
by dani93
Zer0XoL wrote:Ooh, that is really usefull feature! :shock2:
I can´t thank you more :) :worship:
You're welcome :lol:

Ewan wrote:Well, mainly because with pointers you have to use -> to access member functions, and when passing the argument you have to put &Slagor or whatever instead of just Slagor. Not that references are always better, but this time they just made more sense. :mrgreen:
True :)

Ewan wrote:Btw, when I said "do this instead" I was saying instead of his original code - I hadn't seen your reply.
Yeah, I know, cause saw your EDIT (and even quoted it) ;)

Re: N00bish function wont work with class thingy

Posted: Mon May 04, 2009 3:12 pm
by MarauderIIC
You can reseat a pointer, ie, make it point to something else. You can also delete what it points to. If you shouldn't be able to delete what the pointer points to, you should use a reference.

Re: N00bish function wont work with class thingy

Posted: Tue May 05, 2009 9:14 am
by Zer0XoL
MarauderIIC wrote:You can reseat a pointer, ie, make it point to something else. You can also delete what it points to. If you shouldn't be able to delete what the pointer points to, you should use a reference.
Ill use a reference :)

Re: N00bish function wont work with class thingy

Posted: Wed Sep 16, 2009 9:31 am
by newbie1234
Zer0XoL wrote: void SubHP(int p)
{
hp = hp - p;
}
And offcourse, you could use hp -= p; ;) :bow:

Re: N00bish function wont work with class thingy

Posted: Wed Sep 16, 2009 9:38 am
by dandymcgee
newbie1234 wrote: And offcourse, you could use hp -= p; ;) :bow:
Dude last post was Tue May 05, 2009. Unless you have something you feel would be vital to the archival of 4 month old topic, please at least make an effort to resist the urge to reply to it. Thanks. ;)

Re: N00bish function wont work with class thingy

Posted: Wed Sep 16, 2009 9:39 am
by newbie1234
It was lag.