Passing Objects to Functions (Help!)

Started by
17 comments, last by phil05 20 years, 1 month ago
I want to pass an object to a function using c++, however, I''m not having much luck. Here''s an example of what I mean...

#include <iostream>

class common
{
public:
    unsigned short Health;
};

int main()
{
    using namespace std;
   
    common Player;
    Player.Health = 10;
 
    FightSession(Player.Health);

    return 0;
}

FightSession(Player.Health)
{
    using namespace std:
 
    cout << Player.Health << endl;
}
 
Advertisement
This: FightSession(Player.Health)
Should probably be something like this:
FightSession(int health)

Assuming Health is an integer. Also, the cout should replace "Player.Health" with "health". Is that what you are going for?

I've been using Java lately, so I don't exactly remember how it works in C++, but I think you pass objects just like you'd pass any other variable. So, if you want to pass Player itself, you'd do something like:
void main {
FightSession(Player);
}
FightSession (cPlayer some_player) {
... stuff...
}

[edited by - Sirveaux on March 19, 2004 7:02:33 PM]

[edited by - Sirveaux on March 19, 2004 7:03:19 PM]
When you pass player.health, lets assume that you want to do so by value instead of by reference. In that case, the parameter list of the function FightSession, should look like this:

FightSession(unsigned short x)
{
cout << x << endl;
}

Just that simple. As to why you put using namespace std; within the function body I really can''t speculate. But refrain from that!
Well, R2D22U2..
No, I need to pass an object, not a variable.
quote:Original post by philvaira
No, I need to pass an object, not a variable.


ok. Well just pass the whole object you created from class common then, like so:

FightSession(common Player)
{
cout << Player.Health << endl;
}

You can also do it by reference to cut on memory overhead:

FightSession(common& Player)
{
cout << Player.Health << endl;
}
Well, R2D22U2..
Ah. I didn''t pay attention to the first half of that code apparently. Yes. What Nervo said... which is basically what I said, but... you know... correct. Heheh.
This isn't working still... {updated}

#include <iostream>class Common{public:	unsigned short Health;};// Prototypevoid PassToFunc(Common Player);int main(){	using std::cout;	using std::cin;	using std::endl;	Common Player;	Player.Health = 10;	// Pass this to the function	PassToFunc(Common Player);	return 0;}// Functionvoid PassToFunc(Common Player){	using std::cout;	using std::cin;	using std::endl;	cout << Player.Health << endl;}  


[edited by - philvaira on March 19, 2004 7:16:03 PM]
There are only a few small mistakes.

The FightSession function is incorrect. If you only wish to pass the health value then change the function to:

void FightSession(unsigned short Health){    using namespace std:    cout << Health << endl;}


This is because Player.Health isn''t a type, it is a variable. You need to give the parameter the same type as the one you are passing to it.

The function also needs a return type, I put in ''void'' as you weren''t returning anything anyway, but it can be anything you want.

Also FightSession has to be declared before it is used - so move the function to before main. You could also use a prototype. This means that you leave the FightSession function where it is and put the line

void FightSession(unsigned short Health);

before main. This allows to to declare the function at the beginning of the page and define it at the bottom.

Hope that helps.
philvaira, using declarations don''t go in the function body like I said the first time. Secondly, you haven''t corrected your code according to how I showed you.
Well, R2D22U2..
To pass the object itself: (in this case by const reference)

#include <iostream>using namespace std;class common{  public:        unsigned short Health;};void FightSession(const common& ref){        cout << ref.Health << endl;}int main(){        common Player;        Player.Health = 10;         FightSession(Player);        return 0;} 

This topic is closed to new replies.

Advertisement