C++ - 取消引用作为数组元素的指针?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
*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 ofDeck
.您正在传递
pCards[i]
,它是一个指向PlayingCard
(=PlayingCard *
) 的指针。不会有operator<<
方法重载,因此您需要*(pCards[i])
但您还必须确保具有相应的重载类PlayingCard
。即带有签名的友元函数:哎呀,只需阅读您的评论:
您确定该方法对您在上面显示的代码的函数可见吗?
You are passing
pCards[i]
which is a pointer toPlayingCard
( =PlayingCard *
). There won't beoperator<<
method overloaded for that so you need*(pCards[i])
but then you must also ensure that you have the commensurate overloading for classPlayingCard
. ie a friend function with signature:Oops, just read your comments:
Are you sure that method is visible to the function you have shown code for above?