An "intermediary" class that gets a pointer from one object and sends it to another?
Hey.
I know this must be possible, but I'm not really sure what I'm doing wrong.
I have a class "a game", that has "players" and each player own an object called "result".
What I want to do is:
Within the game, call a function that returns a pointer to the Result object and from an output class display what's in the Results.
Is this possible?
1. Game has the players, the players has the result
2. Game calls an output function that "expects" a pointer to a Result object, and as a parameter to that function calls the Player function that returns the Result object as a pointer.
Some code examples:
Command issued within Game:
- InputOutput.showResults( vPlayer.getResult() );
Function within Player:
Result * Player::getResult()
{
// cResult is an object within the Player class, is a part of the prototype
Result *cRes = &cResult;
return cRes;
};
Function within InputOutput:
void InputOutput::showResults(Result* cResult)
{
cout << *cResult.getTotalSum();
}
Everything should work, but doesn't.
I'm using Code::Blocks 10.05, and the error I get is:
error: request for member ‘getTotalSum’ in ‘cResult’, which is of non-class type ‘Result*’|
So, what exactly is going wrong here?
I'm not very good at C++ but I understand everything except the pointers "good".
Anyone with suggestions or ideas, or can explain how I'm thinking wrong? Or is it just Code::Blocks?
Thanks anyway!
Re: An "intermediary" class that gets a pointer from one object and sends it to anoth
cout << cResult -> TotalSum();
The dot operator is not used with pointers. You are suffering problems because of operator precedence. When using pointers use the arrow operator -> not the dot operator.
Re: An "intermediary" class that gets a pointer from one object and sends it to anoth
*cResult.getTotalSum() will attempt to dereference the value returned by getTotalSum rather than the pointer cResult. Use (*cResult).getTotalSum() or, preferably, cResult->getTotalSum().