0

这适用于我比较类的对象时:

//first number is row, then column, then a value which doesnt get compared.
Element e1 (4, 2, 4);

Element e2 (5, 2, 4);

if (e1 > e2)
    cout << "e1 is greater than e2" << endl;
else
    cout << "e1 is not greater than e2" << endl;

bool Element::operator > (const Element& e) const
{
    return ((this -> row == e.row) && (this -> col > e.col)) || (this -> row > e.row);
}

...但不是当我将它们放在链表节点中之后进行比较时。

if (curr -> e > head -> e /*<---- STOPS WORKING HERE*/ || curr -> e == head -> e /* <---- THIS == OPERATOR WORKS HOWEVER*/)
{
    cout << "inserted befrore not empty head" << endl;
    newNode -> next = head;
    head = newNode;
}

当我尝试curr -> ehead -> e. 我不知道为什么,但是我有另一个运算符重载函数来检查两个节点是否相等,并且在使用节点进行比较时似乎可以正常工作。这里仅供参考。

bool Element::operator == (const Element& e) const
{
    return ((this -> row == e.row) &&
            (this -> col == e.col));
}

比较两个节点时,>运算符重载函数给我一个运行时错误。解决方案?

4

0 回答 0