N00bish function wont work with class thingy

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

N00bish function wont work with class thingy

Post 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". =/
Image
Im Blue
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

Re: N00bish function wont work with class thingy

Post 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.
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: N00bish function wont work with class thingy

Post 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:
Last edited by Zer0XoL on Mon May 04, 2009 1:06 pm, edited 1 time in total.
Image
Im Blue
Ewan
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Mon Mar 23, 2009 11:46 am

Re: N00bish function wont work with class thingy

Post 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 :)
They pull out the truncheon, that's when the trouble starts.

'Cause when you've got a badge, the laws don't apply.
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: N00bish function wont work with class thingy

Post 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) :)
Image
Im Blue
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

Re: N00bish function wont work with class thingy

Post 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 ;)
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: N00bish function wont work with class thingy

Post 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:
Image
Im Blue
Ewan
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Mon Mar 23, 2009 11:46 am

Re: N00bish function wont work with class thingy

Post 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.
They pull out the truncheon, that's when the trouble starts.

'Cause when you've got a badge, the laws don't apply.
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

Re: N00bish function wont work with class thingy

Post 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) ;)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: N00bish function wont work with class thingy

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: N00bish function wont work with class thingy

Post 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 :)
Image
Im Blue
newbie1234
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri May 15, 2009 9:01 am
Current Project: I'm currently making my own GUI
Favorite Gaming Platforms: Err... Linux?
Programming Language of Choice: C++
Location: St. Petersburg, Russian Federation

Re: N00bish function wont work with class thingy

Post by newbie1234 »

Zer0XoL wrote: void SubHP(int p)
{
hp = hp - p;
}
And offcourse, you could use hp -= p; ;) :bow:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: N00bish function wont work with class thingy

Post 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. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
newbie1234
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri May 15, 2009 9:01 am
Current Project: I'm currently making my own GUI
Favorite Gaming Platforms: Err... Linux?
Programming Language of Choice: C++
Location: St. Petersburg, Russian Federation

Re: N00bish function wont work with class thingy

Post by newbie1234 »

It was lag.
Post Reply