[RESOLVED] One object accessing another's data?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] One object accessing another's data?

  1. #1
    Join Date
    Apr 2010
    Posts
    19

    [RESOLVED] One object accessing another's data?

    Hi everyone,

    I'm a moderate-level C++ programmer who is a little rusty at the moment. I've got an object question which is driving me nuts. I'm sure this is a C++ 101 level question, but for the life of me, I can't recall the solution. Basically, I've got one object which has to access private data in another object... and can't.

    Here's the specifics: I'm writing a little war game program where players deploy units (soldiers, tanks, planes, etc.) onto a gameboard. Players and Units are modeled as objects:

    Code:
    class GameUnit {
      public:
        string GetName()  {return Name;}
      protected:
        string Name;
    };
    
    
    class Player {
      public:
        void ListUnits();
      protected:
        vector<GameUnit*> MyUnits;
    };
    
    void Player::ListUnits() {
      for(unsigned int i=0; i<MyUnits.size(); i++) {
        cout<<MyUnits[i]->GetName()<<"\n";
      }
    }
    Here's the problem: In the above code, Player's ListUnits() function doesn't work because Player can't access GameUnit's GetName() function. Specifically, here's the compiler's error message:

    Code:
    In file included from Main.cpp:18:
    Player.h: In member function 'void Player::ListUnits()':
    Player.h:47: error: 'GetName' undeclared (first use this function)
    Player.h:47: error: (Each undeclared identifier is reported only once for each function it appears in.)
    I've tested enough to realize that the problem is the GameUnit::GetName() function is a public function within the GameUnit object. Why can't a Player call this function? Making both friend classes of each other doesn't help. Do I have to make these guys related somehow?

    I'm sure this is a fairly simple problem, but I can't find the solution for the life of me.

    Many thanks,
    -P

  2. #2
    Join Date
    Apr 1999
    Posts
    27,426

    Re: One object accessing another's data?

    Quote Originally Posted by phummon View Post
    Basically, I've got one object which has to access private data in another object... and can't.
    1) I see no data that's private. Everything you've posted is either public or protected.

    2) If you're deriving classes from GameUnit, you need to have GameUnit have a virtual destructor.

    3) I have no problem compiling this code on the Comeau C++ compiler. Maybe you should post a complete example of what you're attempting to compile instead of snippets.
    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    
    class GameUnit 
    {
      public:
        std::string GetName()  {return Name;}
    
      protected:
        std::string Name;
    };
    
    
    class Player 
    {
      public:
        void ListUnits();
    
      protected:
        std::vector<GameUnit*> MyUnits;
    };
    
    using namespace std;
    
    void Player::ListUnits() 
    {
      for(unsigned int i=0; i<MyUnits.size(); i++) 
        cout<<MyUnits[i]->GetName()<<"\n";
    }
    This is a complete module, and there is no error in compiling it.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: One object accessing another's data?

    Quote Originally Posted by phummon View Post
    Why can't a Player call this function?
    Maybe you have GameUnit on a separate include file and just forgot to include it.

  4. #4
    Join Date
    Apr 2010
    Posts
    19

    Re: One object accessing another's data?

    Thanks for the tips, the solution was to indeed double-check the include files and make sure they were in a logical order. I know that sounds simplistic, but sometimes you can only spot these problems when a second set of eyes verifies your code.

    Many thanks!
    -P

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center