简体   繁体   中英

C++ - dereferencing pointers that are elements of an array?

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?

*pCards[i] , (*pCards[i]) and *(pCards[i]) are all dereferencing the objects. There is something else going wrong in another part of your program, probably in the implementation of Deck .

ostream& operator<< (ostream& out, const Deck& d)
{
    PlayingCard** pCards = d.getPlayingCards();
    for(int i = 0; i < d.getTotalCards(); ++i)
        out << (*(pCards[i])) << endl;  
    return out;
}

You are passing pCards[i] which is a pointer to PlayingCard ( = PlayingCard * ). There won't be operator<< method overloaded for that so you need *(pCards[i]) but then you must also ensure that you have the commensurate overloading for class PlayingCard . ie a friend function with signature:

ostream& operator<< (ostream& out, const PlayingCard& d);

Oops, just read your comments:

        // the << operator is also overloaded in the PlayingCard class to take PlayingCard objects as arguments.
        // the overload in the PlayingCard class definitely works.

Are you sure that method is visible to the function you have shown code for above?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM