#### Archived

This topic is now archived and is closed to further replies.

This topic is 5326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
Yes!
You can send "outside" parameters to class function!!

If you got some errors then post the code and the actual error messages.

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
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)

##### Share on other sites
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!}

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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.

##### Share on other sites
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]

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10

• 14
• 13
• 14
• 73
• 22
• ### Forum Statistics

• Total Topics
632143
• Total Posts
3004331

×