How to show contents of pointer when in an array

I have an array of pointers to objects (same type). But whenever i print the pointer is shows only the address. How do I make it show the contents?

**Only part of the program**

This is where i created the array of object pointers:
1
2
3
4
5
6
7
  //West Kingdom Monsters
    GameEntity *weaselWest = new GameEntity("weasel West", "Weasel can eat chickens!");
    GameEntity *chickenWest = new GameEntity("chicken West", "Chicken can eat centipedes!");
    GameEntity *centipedeWest = new GameEntity("centipede West", "Centipede can sting weasels!");
    
    //Array of West Kingdom Monster Pointers
    GameEntity *wKingdomPtrArr[3] = {weaselWest, chickenWest, centipedeWest};


This is where im trying to print out the contents of pointer not the address
1
2
3
4
5
  else if (kingdomArr[randNumber] == "WEST")
    {
        int randomWestMonster = Utility::randomNumGenerator(NumMonsters);
        cout << "The West monster battling is: " << nKingdomPtrArr[randomWestMonster];
    }
Dereference the pointer.
1
2
  cout << "The West monster battling is: " << *nKingdomPtrArr[randomWestMonster];
//                                            ^-- Note the asterisk 


edit: Answer incomplete. See post below.
Last edited on
It seems your pointers point to classes. Do these classes have a public member function that returns something that can be printer?

For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Example
{
     public:
          std::string getName()
          {
                return "unknown name";
          }
};


int main()
{
     Example* pointerToMyExample = new Example();
     std::cout << pointerToMyExample->getname();
}


cout will print everything you pass it as good and usefull as possible, but if you pass it something like a pointer it doesn't know what to do with that and it can only print the memory location. If you want something more useful you will have to provide a function that converts the pointer to something that cout can handle.
Last edited on
Abstraction anon- i get an error: invalid operands to binary expression.

Nico - There is no print function in my game entity class. I can show you the code for it if it will help. Shoudnt cout still show the contents of the pointer if its dereferenced via * operator? So shouldnt there still be a way to do this without creating a function?
Sorry, I only gave you a partial answer. Dereferencing the pointer assumes that the objected pointed to can be sent to cout. I was thinking your pointers were pointers to strings. My mistake.

To be able to cout a GameEntity, the GameEntity class must overload the output operator.

1
2
3
4
5
//  Inside GameEntity
friend ostream & operator << (ostream & os, const GameEntity & ge)
{  //  format ge to ostream
    return os;
}


AbstractionAnon- I am not quite sure what you've done. I think it maybe above what i have learned which makes me believe i may have made an error somewhere else in the code?

But could you explain what that function does and how i would call it in main?
As you should know, C++ has the ability to overload operators. What I did was to add a << operator to your GameEntity class. cout doesn't know how to format a GameEntity, therefore you have to provide an overloaded operator << function to do so.

Since you haven't shown the declaration for GameEntity, I have no idea what is in your class, which is why I left line 3 as a comment. Let's assume GameEntity has a name member. and when you cout a GameEntity, you want to display the GameEntity's name.

The friend function would become:
1
2
3
4
5
//  Inside GameEntity
friend ostream & operator << (ostream & os, const GameEntity & entity)
{  os << entity.name;           // Display member variable 
    return os;                         // Return reference to stream (required)
}


Then when you do the following:
1
2
3
4
  GameEntity  entity ("monster");  //  Let's assume constructor takes a name
...
  cout << "You're being chased by a " << entity << endl;
//                                    ^^^^^^^^ Calls entity's << operator 

The following would be displayed:

You're being chased by a monster


If you're dealing with pointers, the usual dereferencing rules apply:

1
2
3
4
5
 
  GameEntity  *entity  = new GameEntity ("monster");   // Pointer to a GameEntity
...
  cout << "You're being chased by a " << *entity << endl;
//                                    ^^^^^^^^ Note the * dereferencing entity. 
Last edited on
Thank you for explaining it to me! i found examples online but was having a difficult time applying it to my program. I appreciate it!
Topic archived. No new replies allowed.