Archived

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

phil05

Passing Objects to Functions (Help!)

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
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
You need to you pointers like below. Otherwsie your copying the Object to another object that only has scope inside the function that its pass to. It does not maintain the value....


#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* player)
{
using namespace std:

cout << player->Health << endl;
}

Share this post


Link to post
Share on other sites
Darn it, it worked on a seperate program. Now I try the main program and it is saying...

Compiling...
Pract1.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice\Pract1.cpp(6) : error C2143: syntax error : missing '','' before ''&''
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice\Pract1.cpp(6) : error C2059: syntax error : ''&''
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice\Pract1.cpp(26) : error C2664: ''fightSession'' : cannot convert parameter 1 from ''class Common'' to ''const int''
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice\Pract1.cpp(122) : error C2166: l-value specifies const object
Error executing cl.exe.

Practice.exe - 4 error(s), 0 warning(s)


Here''s the code:

#include <iostream>
#include <ctime>

randFunct();
void randYouMiss();
fightSession(const Common& ref);

class Common
{
public:
int Health;
};

int main()
{
using std::cout;
using std::cin;
using std::endl;

// Player Health
Common Player;
Player.Health = 10;


// Fight: Orc
Player.Health = fightSession(Player);
if (Player.Health == 0)
{
cout << "\n*** Game Over ***\n" << endl;
system("pause");
exit(1);
}
else
{
cout << "You currently have " << Player.Health << " hit point";
(Player.Health > 1) ? cout << "s. " : cout << ". " << endl;
}



system("pause");

return 0;
}


randFunct()
{
using std::cout;
using std::cin;
using std::endl;

unsigned short int randNum;
unsigned short int randNumMax = 20;

// Random Number Generator
time_t t;
srand(time(&t));
randNum = (rand() % (randNumMax));
return randNum;
}

void randYouMiss()
{
using std::cout;
using std::cin;
using std::endl;

switch(randFunct())
{
case 2: cout << "The orc laughs at your wimpy swing.\n" << endl;
break;
case 3: cout << "You swing but miss horribly!\n" << endl;
break;
case 4: cout << "You swing as the orc ducks sucessfully.\n" << endl;
break;
case 5: cout << "Swing at the orc. Not the tree!\n" << endl;
break;
case 6: cout << "Your tremendous anger causes you to miss horribly.\n" << endl;
break;
case 7: cout << "You miss again.\n" << endl;
break;
case 8: cout << "While you miss, the orc shouts, \"Come off! Show me what you humans can do!\"\n"<< endl;
break;
case 9: cout << "Sweat overcomes your tired body.\n" << endl;
break;
default: cout << "You miss!\n" << endl;
}
}

fightSession(const Common& ref)
{
using std::cout;
using std::cin;
using std::endl;
// Stats
int orcHP = 10;

// Fight Orc
while (orcHP != -1)
{
cout << "Your Health: " << ref.Health << endl;
cout << "Orc''s Health: " << orcHP << endl;
cout << "================\n" << endl;

if (randFunct() >= 15)
{
cout << "You hit him!\n" << endl;
orcHP--;
if (orcHP == 0)
{
cout << "\nThe orc falls to his death." << endl;
}
system("pause");
system("cls");
}
else
{
randYouMiss();
if (randFunct() >= 12)
{
ref.Health--;
cout << "The orc hits you!\n" << endl;
if (ref.Health == 0)
{
cout << "\nYou fall to your death." << endl;
}
}
system("pause");
system("cls");

}
}
return ref.Health;
}

Share this post


Link to post
Share on other sites
ok, back up for a second. those errors you are getting actually point DIRECTLY to the problem with your code. see those numbers in parenthesis:

C:\Program Files\Microsoft Visual Studio\MyProjects\Practice\Pract1.cpp(6) : error C2143: syntax error : missing ',' before '&

(6) is the line number where the problem is. so look at line #6 and try to see what it's complaining about.

anyway, the problem is that the compiler is order sensitive. when you predefine your functions up above you haven't yet defined the class Common. so the compiler doesn't know what to do with the word common there in line 6. move the function prototypes below the class Common decleration and you'll get past those errors.

you'll probably have other errors, but try looking at the line numbers and see if you can figure out what is wrong.

-me

[edited by - Palidine on March 19, 2004 7:45:27 PM]

Share this post


Link to post
Share on other sites
OOH HAHA. I never thought of that. Your right.. the class was below the prototype. Prototypes, I assume, should always be above main, yet below the classes... I got one error left.

C:\Program Files\Microsoft Visual Studio\MyProjects\Practice\Pract1.cpp(124) : error C2166: l-value specifies const object
Error executing cl.exe.

Practice.exe - 1 error(s), 0 warning(s)


ref.Health--;


Here, I''m trying to decrement the player''s health. Any suggestions?

Share this post


Link to post
Share on other sites
Change the function argument from const Common & to Common &. const means you promised not to modify it, but you want to modify it, so making that promise makes no sense.

Share this post


Link to post
Share on other sites
Oh yeah.. hehe So there is logic in it. It works!! Thanks

[edited by - philvaira on March 19, 2004 7:52:30 PM]

Share this post


Link to post
Share on other sites
Yep, if you're going to modify an object's members. You should use the following function declaration:

int fightSession(Common& ref)

Notice i removed the const.
btw: i see that you want to return ref.Health. So be sure to add the int return type in front of your function declaration.


Edit: Lol, you guys are so fast... My posts are outdated even before i finnish typing them.. hehe


[edited by - Direct4D on March 19, 2004 7:54:20 PM]

Share this post


Link to post
Share on other sites