0

I have been trying to make use of operator overloading in my program, however I have been running into issues.

Basically, I have a binary search tree and I am trying to get it so that if you enter something like "tree = tree + 7", it will add the key value 7 as a node. I have been experimenting with the code to do this:

struct addToTree {
    string treeName;
    int value;
    string output;

    string operator+(string addToTree &lhs, int addToTree &rhs) {
        addToTree temp;
        temp.output = lhs.treeName + to_string(rhs.value);
        return temp.output;
    }
};

At the moment, I am just trying to get it to take the name of the tree, and add to that the value that the user enters to add to the tree.

I am currently getting a number of errors and I don't think my code is even close to how it should be so if anyone could give me advice to show me what I should be doing, that would be much appreciated.

user3746428
  • 11,047
  • 20
  • 81
  • 137
  • `int addToTree %rhs` ? Show your real code. Your `operator+` is shown as returning an `addToTree` and yet it's clearly returning a `string`. Is that really how it is or just another typo? – Jonathan Potter Mar 19 '15 at 10:50
  • That should have been &rhs, but I don't know if that makes any more sense or not, and it should be returning a string for testing purposes. I've fixed it now. – user3746428 Mar 19 '15 at 10:52
  • 1
    This kind of operator overloading is almost always harmful. Your operators should have the same semantics as the standard versions, otherwise you and anyone who looks at your code later are in for some head-scratching errors. "do as the ints do" – TartanLlama Mar 19 '15 at 10:53
  • @user3746428 Your code won't even compile! `string operator+(string addToTree &lhs, int addToTree &rhs)` is `lhs` a `string` or an `addToTree`? How about `rhs`? If they're both `addToTree`s this should be defined outside your `struct addToTree`, or if inside it should only take 1 argument. – Jonathan Mee Mar 19 '15 at 10:59
  • If you are passing arguments by reference, pass them as const if they are not modified. – Neil Kirk Mar 19 '15 at 11:17
  • Linked duplicate explains how to do all of this. – M.M Mar 19 '15 at 11:18

1 Answers1

0

operator + is a binary operator , meaning it get 2 arguments as operands. you can implement it either as free function or member function

as member function , it gets only one argument , the element you want to add:

returnValue SomeClass::operator + (const SomeType& addee);

in the above example, the first added element is always the object that holds the + operator inside.

as free function, it gets two argument - the two elements you'd like to sum up:

returnValue operator + (const SomeType1& addee1, const SomeType2& addee2);

here, you mixed member function syntax (example 1) with free standing function (example 2) you need your member overload to get only 1 argument (or implement it as free standing function).

also , the line (string addToTree &lhs, int addToTree &rhs) is syntactically wrong what does string addToTree &lhs means? is it a string? is it something else ? the expression string addToTree has no meaning since it doesn't represent any type

David Haim
  • 25,446
  • 3
  • 44
  • 78