0

This works for when I'm comparing objects of a class:

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

... but not when I compare it after I have them in a linked list node.

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

The program stops running when I try comparing curr -> e to head -> e. I'm not sure why but I have another operator overload function that checks if the two nodes are equal, and that seems to be working when compared using nodes. Here it is just for reference.

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

The > operator overload function gives me a runtime error when comparing two nodes. Solutions?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
TL Han
  • 27
  • 1
  • 10
  • @alestanis It just crashes. No error output. – TL Han Feb 14 '13 at 14:36
  • just a guess since you didn't post the error but I think you're comparing nodes or pointers instead of Elements – eladidan Feb 14 '13 at 14:36
  • Then how do you know it's a runtime error and not something else? – alestanis Feb 14 '13 at 14:37
  • "The compiler stops running when I try comparing curr -> e to head -> e." what do you mean no error? Is this a runtime problem or compilation problem? – eladidan Feb 14 '13 at 14:39
  • @alestanis I'm not completely sure what kind of crash it is. I'm just a beginner coder if you didn't already figure it out. I'm using xCode and my code runs fine and outputs everything right until it gets to the comparison part. It outputs (lldb) and it highlights the bool function with the '>' operator overload. – TL Han Feb 14 '13 at 14:40
  • Perhaps one of the pointers `curr`, `head` is null, or points to garbage memory? – Angew is no longer proud of SO Feb 14 '13 at 14:41
  • It does however like I've written in my question, runs the '==' overload operator function as expected, but not the '>'. – TL Han Feb 14 '13 at 14:42
  • @AceHahn Then you mean the **program** stops runnig, not the compiler. – Angew is no longer proud of SO Feb 14 '13 at 14:42
  • You have an error message somewhere. I'm not familiar with XCode, you should look it up on the internet to see the error message. If you don't give it to us I'm afraid we won't be able to help. – alestanis Feb 14 '13 at 14:43
  • @Ace: what is the type of curr -> e and head -> e ? Is it Element?Element*? Element&? If its Element* then by using the operator > you are comparing pointers anot Elements and thus the problem – eladidan Feb 14 '13 at 14:43
  • @AceHahn If you are dereferencing null or invalid pointers, you obtain *undefined behaviour.* In such case, the program is free to do literally anything (including crashing on `>` but running fine on `==`). Once you get UB, all bets are off. – Angew is no longer proud of SO Feb 14 '13 at 14:44
  • Anyway, you should consider grabbing a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Angew is no longer proud of SO Feb 14 '13 at 14:45
  • @AceHahn: how do you know it runs the `==` as expected? Did you remove the other check? If it crashed on `>`, it won't ever reach `==`. In any case, check the value of the variables in the debugger, that should give you your answer. – Jack Aidley Feb 14 '13 at 14:53
  • Have you tried using a debugger to see the exactl line, operation and the values of the variables involved? Have you run a tool like valgrind to check for memory access errors? – PlasmaHH Feb 14 '13 at 16:10
  • Hey guys, the bool overload code is alright! Looks like I had a logical error with the pointers when I was setting up my link lists. – TL Han Feb 14 '13 at 17:26

0 Answers0