比较运算符重载模板节点值

时间:2013-02-14 14:33:33

标签: c++ overloading nodes operator-keyword

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

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

>运算符重载函数在比较两个节点时给出了运行时错误。溶液

0 个答案:

没有答案