0

I may not be doing this correctly but is it possible to get multiple return values from an overload? I am using an overload to compare between objects in a Binary Search Tree.

I have about 5 of them, but I will post two (they are all similar anyways).

bool ExtPersonType::operator > (ExtPersonType bob){
     return (this->getLastName() > bob.getLastName());
     //return (getDate().getMonth() > bob.getDate().getMonth());
     //return (this->getStatus() > bob.getStatus());    
}
bool ExtPersonType::operator==(ExtPersonType bob){
      return (this->getLastName() == bob.getLastName());
      //return (getDate().getMonth() == bob.getDate().getMonth());
      //return (this->getStatus() == bob.getStatus());  
}

So I have three functions that compare objects of "ExtPersonType" in the BST. I tried this as I saw some examples but no luck (the output spits out randomly and data doesn't make sense).

bool ExtPersonType::operator > (ExtPersonType bob){
     return (this->getLastName() > bob.getLastName() &&
            getDate().getMonth() > bob.getDate().getMonth() &&
            this->getStatus() > bob.getStatus());
}

If I do one return statement at a time (i.e commenting out the others). It works perfectly but obviously isn't ideal.

Any thoughts or things I can try? Thanks alot in advance!

Edit: Posting further code.

I have a function (or functions) for that matter in which I take a string or int as input. I pass that to my BST where I use it to search and/or print. The value that I pass in is this->getLastName() which compares to (either <, ==, etc) currentNodeinBinaryTree.getLastName()

void DTBinarySearchTree<T>::search(Node<T> *tree, T value)
{
if(tree){
    search(tree->left, value);    //recursive method that locates matches, checks entire tree
    if( tree->value == value)
        cout << tree->value << endl;
    search(tree->right, value);
    }

I am using the above method to look at all the people that have the same birthdays for example (list all people with January birthdays).

Again as mentioned everything works, so it's not the method but rather the overload that I am having trouble with. Because I have two other functions like this... }

Tacos
  • 21
  • 3
  • 2
    What are you trying to achieve? Please post a [complete example](http://stackoverflow.com/help/mcve) illustrating your problem. – Anton Savin Apr 29 '15 at 12:12
  • Your `>` establishes an ordering relation that looks very suspicious - if two people have the last name, none will compare greater than the other. I doubt that you want that in a search tree. – molbdnilo Apr 29 '15 at 12:18
  • possible duplicate of [Should a function have only one return statement?](http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement) – NathanOliver Apr 29 '15 at 12:22
  • You don't have three functions that compare `ExtPersonType`, you have two, i.e. `>` and `==`. If your first try worked when you comment out the others in turn, they must correctly return `bool`, so I don't see why your second `operator>` that combines each comparison with `&&` wouldn't work. – Phil Wright Apr 29 '15 at 12:31
  • What is the desired result? Do you want the function (or operator if you want) to return three booleans? – hildensia Apr 29 '15 at 12:39
  • The desired result would be... Run FunctionA(string lastName){ //goes to the BST and looks at objects that have this lastName(using <, ==, >, etc – Tacos Apr 29 '15 at 12:42
  • Sorry I got cut off there... The desired result would be... Run FunctionA(string lastName){ //goes to the BST and looks (traverses) at objects that have this lastName(that's where my overload comes in b/c I have to compare objects ( <, ==, >, etc) Run FunctionB(string status){ //again does the same thing as function A but this time the overload operator that I use to compare with objects in BST (<,==, >) should return me (this->getStatus() == bob.getStatus()) so I can obviously do the comparison with the string statuses this time – Tacos Apr 29 '15 at 12:50
  • Someone said people ought to pass certifications like for gun license before they are allowed to use operator overloading. Seeing this example, I'm compelled to agree. – SF. Apr 29 '15 at 12:51
  • I'm just trying to learn after all. Unfortunately we have people like you who instead of helping others feel the need to belittle them. Thanks for your insightful feedback. – Tacos Apr 29 '15 at 12:58

0 Answers0