0
Why my code doesn't work?
Why isn't this function with operator ok? https://code.sololearn.com/cA8A19A248a9/?ref=app
1 Answer
0
If you define operator + inside the class, you implement it in terms of the current object (this) and one other parameter, because it will be called as a member function. For example, the call q1 + q2 becomes q1.operator+( q2 ). You only need two parameters if you define it as a standalone function. Therefore, you either have to change your implementation, or define it outside the class (in which case you will probably want to declare it a friend function).
You are also trying to return a reference to a temporary value. That doesn't make sense, as "temp" will be destroyed at the end of the function and the reference would be left invalid. Return a copy instead.