Jump to content
  • Advertisement

Archived

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

phil05

Passing Objects to Functions (Help!)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}
 

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
This isn't working still... {updated}


#include <iostream>

class Common
{
public:
unsigned short Health;
};

// Prototype
void 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;
}

// Function
void 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!