C++ - 取消引用作为数组元素的指针?

发布于 2025-01-03 06:42:01 字数 895 浏览 0 评论 0原文

我有一个 Deck 类的对象,其中包含一个动态分配的指向另一个类 PlayingCard 的对象的指针数组。我正在尝试超载 <<运算符(作为 Deck 类的友元)迭代输出 Deck 对象中每张卡的详细信息。目前重载定义如下所示:

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;
}

当尝试构造 Deck 对象并输出其卡片详细信息时,它输出内存地址列表而不是实际数据,因此我想我需要取消引用 pCards[i]。然而,当我尝试这样做时,输出是垃圾,最终在调试器中遇到访问冲突。我已经尝试了以下所有组合,但所有组合都会导致编译时或运行时问题:

*pCards[i], pCards[i]*, (*pCards[i]), *(pCards[i])

这是否只是取消引用数组内指针的错误语法,或者是否有更深层次的东西我不理解?我如何重写这段代码,以便程序输出这些 PlayingCard 对象保存的实际数据,而不仅仅是内存地址?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2025-01-10 06:42:01

*pCards[i](*pCards[i])*(pCards[i]) 都取消引用对象。程序的其他部分还存在其他问题,可能是在 Deck 的实现中。

*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.

疯到世界奔溃 2025-01-10 06:42:01
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;
}

您正在传递 pCards[i],它是一个指向 PlayingCard (= PlayingCard *) 的指针。不会有 operator<< 方法重载,因此您需要 *(pCards[i]) 但您还必须确保具有相应的重载类PlayingCard。即带有签名的友元函数:

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

哎呀,只需阅读您的评论:

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

您确定该方法对您在上面显示的代码的函数可见吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文