An "intermediary" class that gets a pointer from one object and sends it to another?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: An "intermediary" class that gets a pointer from one object and sends it to another?

  1. #1
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    26

    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!

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    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().

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center