question about classes

Started by
10 comments, last by graveyard filla 20 years, 3 months ago
is it possible for classes to have internal functions that can access parameters from outside of the class? what im trying to say is, even if a function is public, is it possible to pass it parameters from outside of the class. im just learning about classes now and i cant seem to get the internal functions to recieve any parameters. its saying that its an undeclared identifier. (if i make the variables global it all works fine. but is this the only way?) so can you only pass parameters to class functions from within the class itself? ps i do have everything as public. [edited by - graveyard filla on January 15, 2004 3:26:18 AM]
FTA, my 2D futuristic action MMORPG
Advertisement
Yes!
You can send "outside" parameters to class function!!

If you got some errors then post the code and the actual error messages.
"...and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces."----------Scott Meyers, "Effective C++"
Try specifying the language you use. In C++, Java and Pascal it should not be a problem. And try adding some code too and the exact error message.
is there some special syntax for sending a parameter to a class's fucntion? i have a class called enemy. one of the class's function is attack(). attack will calculate the enemies attack and return the players health. i do
class enemy{ public: void attack();};//then right after that, i would declare and write the functionvoid enemy::attack(int &player_health){	     calculate damage done, set new value for player_health;     player_health = player_health - damage;}enemy monster;

now, somewhere else down in the program is a function that handles all battles. and i get to the part where i want my monster or whatever to attack and put

monster.attack(player_health);

well, everything runs smooth, except player health isnt updated. its not sending back the value and updating. no matter how much damage gets done to me, my health stays the same. what i did was change player_health to a global variable and deleted whereever it was passed around as a parameter. the program ran fine with no problems, health was updated as usual.

this is just a small text based rpg that ive been working on. i just learned about classes so i thought id try implementing classes into the program (so far i see that to fully implement classes properly id have to completely re write the whole program). i just wanted to play around with classes to see if i could do it myself. i ran into this problem.


ps this isnt actualy source code i just tried to sum it up for you all



[edited by - graveyard filla on January 15, 2004 4:19:48 AM]
FTA, my 2D futuristic action MMORPG
What you could do is to make a Player class, and give the enemy::attack function a reference to the player object it is fighting. In the enemy::attack function you could then call a player::damage(int damage) function. In this new function you then update the player''s state.

Did that make sense? Or do you want some source/pseudo-code?


Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
There shouldn''t be anything wrong with what you''re doing except the void attack() in your class definition doesn''t match up with void enemy::attack(int &player_health). You probably forgot something in your actual code. Passing player_health by reference is enough to keep any changes made to it after the function ends.




--{You fight like a dairy farmer!}

--{You fight like a dairy farmer!}

that was just a typo. in the actual code i had it all in there.

also, does the parameters its getting sent have to be from a different class?> or could it be from anywhere in the program? why wouldnt this work for me? argh this is frustrating.
FTA, my 2D futuristic action MMORPG
quote:Original post by rohde
What you could do is to make a Player class, and give the enemy::attack function a reference to the player object it is fighting. In the enemy::attack function you could then call a player::damage(int damage) function. In this new function you then update the player''s state.

Did that make sense? Or do you want some source/pseudo-code?


Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)


psuedo/source would be great. thanks ! . and DO i have to send a classes functions parameters from another class, or can it be from anywhere in the program?
FTA, my 2D futuristic action MMORPG
quote:Original post by graveyard filla
well, everything runs smooth, except player health isnt updated. its not sending back the value and updating.

Presumably because somewhere a copy of player_health is implicitly made and passed to attack. However, since you''re not showing us real code, its not possible to say exactly what you are doing wrong. Hint: don''t pass player_health, pass the entire player object and have attack call member functions on the player object so that it may decide for itself how much damage has been inflicted. That way, you will be able to localise code to the parts of the program where it is relevant.
quote:
ps this isnt actualy source code

Then we can''t actually tell you what the problem is.
how do you pass an entire object? when you pass an object, does it also pass all the objects functions and variables/data with it? or do you have to do it all one by one? do you have to pass the class too, or does it know allready what the class can do?

sorry if these seem like stupid questions. i have only so far learned a little about classes/OOP and it seems hard to grasp at first. but i have a basic understanding of it..... i think?

[edited by - graveyard filla on January 15, 2004 7:32:58 PM]
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement