Need help with passing by ref with pointers

Started by
2 comments, last by finky45 16 years, 11 months ago
I have 2 objects, cPlayer player and cTime time1. I need to access them both in function fHome: The following is NOT the actual files. There are about 6 of them and they are very long...

//my prototypes
void fHome(cPlayer*, cTime*);
void fCar(cPlayer*, cTime*);


main()
{
fHome (&player,&time); //here i call fHome and pass player and time1
}

//function home which works.
void fHome (cPlayer* player, cTime* time1)
{
//bla bla bla

player->setHealth (100); //this works.
time1->printTime();  //works too

//then i *try* call fCar and pass the player and time1 by reference

fCar (&player, &time1); //this is probably wrong...


//function car which doesn't work.
void fCar (cPlayer * player, cTime * time1)
{
//i want to be able to access player and time1 in here too.
//and call in another function which i want to pass the player and time1 again.
}


I get an error: fHome.cpp: In function `void fHome(cPlayer*, cTime*)': fHome.cpp:32: error: cannot convert `cPlayer**' to `cPlayer*' for argument `1' to `void fCar(cPlayer*, cTime*)' make.exe: *** [fHome.o] Error 1 I tried adding replacing * with ** in fCar. After that it seems to work except when I call player->setMoney() i get setMoney undefined...
Advertisement
Don't. In C++, passing objects by reference is done using references (possibly constant ones). For example:

void fHome(cPlayer&, cTime&);void fCar(cPlayer&, cTime&);int main() // Do not forget the return type{  fHome (player,time);}void fHome (cPlayer& player, cTime& time1){  player.setHealth (100);   time1.printTime();    fCar(player,time1);}void fCar (cPlayer& player, cTime& time1){}


Your error message is pretty explicit: it tells you that you are passing an argument of type cPlayer** to a function which expects a cPlayer*. This happens because you are passing the address of a pointer, instead of the pointer itself. Your code would have looked like this:

void fHome (cPlayer* player, cTime* time1){  if (player)  // Don't forget to check for null pointers    player->setHealth (100);   else    ; // Do something when no player is provided    if (time1) // Check for null pointers    time1->printTime();    else     ; // Do something when no time is provided  fCar (player, time1); }


Quote:I tried adding replacing * with ** in fCar. After that it seems to work except when I call player->setMoney() i get setMoney undefined...


Stop that. When you encounter a compiler error, immediately stop modifying your code. Randomly altering your code until it compiles has the heavy disadvantage that you will not understand the problem. Perhaps you managed to compile a program which will crash your computer (yes, this happens) without knowing. And either way, it does not help you learn anything.

The only correct approach to compiler errors is understanding them. If you don't understand the error, you cannot hope to correct it.
The problem is that when you are inside of fHome, both player and time1 are already pointers. So when you make the call to fCar like this:
fCar (&player, &time1);

You are passing the address of player and time1, which means you're passing a pointer to a pointer (a **). Remember, when you have a pointer, the pointer's name is actually the value of it's address, and when you dereference it you get the value (so in this case 'player' is the address and '*player' is the value stored at that address).

If you're passing a pointer to a function that expects a pointer, then all you have to do is this:
fCar (player, time1);

since time1 and player are already pointers.

However, if they weren't pointers, then you would need the & so you could pass the address of those variables to fCar. Then in the function header for fCar, the behavior would be something like this:
//call to fCar() in fHome()fCar (&player, &time1);//What's really happening in fCar's headerfCar(cPlayer* player = &player, cTime* time1 = &time1);

The pointers have been assigned the address of the variables you passed in.

Did any of this make sense? Looking back I have a feeling some of this may sound confusing.
I had to read it a few times over, but it made sense in the end, thanks ChopperDave!

I modified my code and it works now.

This topic is closed to new replies.

Advertisement