I have an object of a Deck class which contains a dynamically allocated array of pointers to objects of another class, PlayingCard. I'm attempting to overload the << operator (as a friend of class Deck) to output details of each card in a Deck object iteratively. At present the overload definition looks like this:
ostream& operator<< (ostream& out, const Deck& d)
{
PlayingCard** pCards = d.getPlayingCards();
for(int i = 0; i < d.getTotalCards(); ++i)
{
out << pCards[i] << endl;
// the << operator is also overloaded in the PlayingCard class to take PlayingCard objects as arguments.
// the overload in the PlayingCard class definitely works.
}
return out;
}
When attempting to construct a Deck object and output its card details, it outputs a list of memory addresses rather than actual data, so I guess I need to dereference pCards[i]. When I try to do that, however, the output is garbage and I eventually reach an access violation in the debugger. I've tried all of the following combos, but all cause either compile-time or run-time problems:
*pCards[i], pCards[i]*, (*pCards[i]), *(pCards[i])
Is this just incorrect syntax for dereferencing a pointer that's within an array, or is there something deeper I'm not understanding here? How can I rewrite this code so the program outputs the actual data held by these PlayingCard objects, rather than just the memory addresses?