重载比较运算符的使用>在 C++与 getter 函数结合使用
我正在努力解决有关二进制比较运算符 > 重载的问题。按照设计,它应该比较两张卡并返回 1(如果左侧参数较大)或 0(相反情况)。
下面是对该问题的简要描述:
class Card
包括变量 intsuit
和 int value
作为私有数据成员。我已将重载运算符函数声明如下:
int operator>(const Card& lhs, const Card& rhs);
因为它需要访问 Card 类的私有数据成员
,它是在类声明中使用 friend
限定符声明的。
该功能本身已被确认可以按描述工作。真正的问题在于通过调用以下形式的“getter”函数来提供两个参数:
Card &Node::getCardRef() const{
Card& ref = *c;
return ref;
}
其中变量 c
的类型为 Card *
并指向一个有效对象类型为卡
。另外,Node 类
的实例表示单链表中的一个节点。
按以下方式组合这两个函数会导致段错误(具体来说,用 gdb 术语“In Card &Node::getCardRef(): this = 0x0”):
if (node.getCardRef() > node.getNext()->getCardRef()){
/* do wondrous stuff */
}
此外,在隔离时,Card &Node::getCardRef ()
似乎产生了预期的结果。
I'm struggling with a problem concerning the overloading of the binary comparison operator >. By design, it is supposed to compare two cards and return either 1 (if the left-hand-side argument is bigger) or 0 (in the opposite case).
Here's a brief description of the problem:
class Card
includes, among other stuff, the variables int suit
and int value
as private data members. I've declared the overloaded operator function as follows:
int operator>(const Card& lhs, const Card& rhs);
Because it needs to access private data members of class Card
, it is declared with the friend
qualifier in the class declaration.
The function itself is confirmed to work as described. The real problem lies with providing the two arguments by calling a 'getter' function of the following form:
Card &Node::getCardRef() const{
Card& ref = *c;
return ref;
}
where the variable c
is of type Card *
and points to a valid object of type Card
. Also, an instance of class Node
represents a node in a singly-linked list.
Combining the two functions in the following manner causes a segfault (specifically, in gdb terms "In Card &Node::getCardRef(): this = 0x0"):
if (node.getCardRef() > node.getNext()->getCardRef()){
/* do wondrous stuff */
}
Also, when isolated, Card &Node::getCardRef()
seems to produce desired results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Node::getCardRef
在该代码片段中被调用两次。第一次是.
运算符的结果,因此我们可以合理地确定*其this
将是有效的。对
Node::getCardRef
的另一个调用是->
运算符的结果。->
的左侧完全有可能是 0(因此,this
也将是 0)。node.getNext()
很可能返回 0。单链表通常通过返回空指针来指示列表结束条件。我猜您正在将链接列表中的最后一项与不跟随它的空项进行比较。
*: we can be reasonably certain, but not 100% certain. It is possible that node contains a corrupt reference, or that a previous wild pointer has corrupted our local variables. In my experience, null pointers are much more likely than null references.
Node::getCardRef
is called twice in that code fragment. The first time as a result of the.
operator, so we can be reasonably certain* that itsthis
will be valid.The other call to
Node::getCardRef
is the result of the->
operator. It is entirely likely that the left-hand-side of the->
is 0 (thus,this
will be 0, also).It is very likely the case that
node.getNext()
is returning 0. Singly-linked lists usually indicate the end-of-list condition by returning a null pointer.I guess that you are comparing the final item on your linked list with the null item that doesn't follow it.
*: we can be reasonably certain, but not 100% certain. It is possible that node contains a corrupt reference, or that a previous wild pointer has corrupted our local variables. In my experience, null pointers are much more likely than null references.