Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Fuction problems

I want to use the public set and get member functions to obtain private values from a class. I am able to execute these functions within main(). What I want to do is use these class member functions to manipulate private vairables from functions inside main(). When I try to do this in a function it wants me to initialize the class object again which causes the object and class values to be local to that function. I have the class object set as a pointer. Here is an example of one of the functions in my program.

status()
{
Player * prandom = new Player;
char cont;
while (cont != 'c')
{
std::cout << "\n" << prandom->getresults1() << " results1!!\n";
std::cout << "\n" << prandom->getresults2() << " results2!!\n";
std::cout << "\nPress c and enter to continue :";
std::cin >> cont;
}
return 0;
}

The "Player * prandom = new Player;" I think is incorrect here since it is also needed in main(). Any suggestions would be great.
[1117 byte] By [davecards] at [2007-11-11 8:50:11]
# 1 Re: Fuction problems
If your Player object is already created in main function, and you need to manipulate it in status function, then you can post it as a parameter:

void status( Player * prandom)
{
// access prandom here, e.g.: "prandom->getresults1() "
// ...
}

void main()
{
Player * prandom = new Player;
// ...
status(prandom);
// ...
}

Or, without pointers:

void status( Player & player)
{
// access prandom here, e.g.: "player.getresults1() "
// ...
}

void main()
{
Player player;
// ...
status(player);
// ...
}
Viorel at 2007-11-11 21:00:57 >
# 2 Re: Fuction problems
Appriciate the reply. I did what you said but had to use
functionname(*prandom); instead prandom to call the fuctions from main().
I was getting errors when i just used prandom as the parameter.

These are the two errors i'm getting now.

"Error: Unresolved external 'start(Player)' referenced from C:\DOCUMENTS AND SETTINGS\DAVID\DESKTOP\RPG\WINDOWS\DEBUG_BUILD\RPGSOURCE.OBJ"

"Error: Unresolved external 'menu(Player)' referenced from C:\DOCUMENTS AND SETTINGS\DAVID\DESKTOP\RPG\WINDOWS\DEBUG_BUILD\RPGSOURCE.OBJ"

Here is a small expample of how my code is setup.

menu(Player);
status(Player);
start(Player);

int main()
{
Player * prandom = new Player;

start(*prandom);
menu(*prandom);

return 0;
}

start(Player * prandom)
{

//this fuction is called after some code but i removed that.
status(*prandom);

return 0;
}

status(Player * prandom)
{
char cont;
while (cont != 'c')
{
std::cout << "\n" << prandom->getresults1() << " results1!!\n";
std::cout << "\n" << prandom->getresults2() << " results2!!\n";
std::cout << "\nPress c and enter to continue :";
std::cin >> cont;
}
return 0;
}

menu(Player * prandom)
{
//this fuction is called after some code but i removed that.
status(*prandom);

return 0;
}

Any ideas how to get around the two errors i'm getting?
davecards at 2007-11-11 21:02:04 >
# 3 Re: Fuction problems
Hi,
The error you are getting is a linker error. I think you probably did not link the *.obj files that contain the compiled code for your functions start(...) and menu(...).
Cheers,

Dieter
drkybelk at 2007-11-11 21:03:02 >
# 4 Re: Fuction problems
Your function declarations are wrong. You need to provide a return value, or void, and you need to specify the that parameter is passed by address or by reference, not by value. In short,
menu(Player);
status(Player);
start(Player);

Should read:

void menu(Player & );
void status(Player& );
void start(Player& );

In main, change the definition of the Player object to:
int main()
{
Player p;

menu (p);
}
Danny at 2007-11-11 21:04:02 >
# 5 Re: Fuction problems
I went ahead and removed the pointers and passing object as parameter from main(). It works fine then. Since the class object is declared in main() and this being a small program there isn't any reason to make it a pointer?
davecards at 2007-11-11 21:05:07 >
# 6 Re: Fuction problems
yup, there's no need to use pointers here at all.
Danny at 2007-11-11 21:06:06 >