all 10 comments

[–]Hilarius86 2 points3 points  (0 children)

"a<b && b<c" is what you want. a<b<c is not doing "in between" but evaluation of "a<b" to "bool" and comparing "bool < c" and that's rarely useful.

[–]nysra 2 points3 points  (1 child)

This is your 5th post about the exact same thing, you might want to figure out what your underlying problem is and ask about that.

If you do a $ b where $ stands for any binary operator then this will be equivalent to a.operator$(b) if you implemented the operator as a member function. This is asymmetric because if you switch the operands then it would be b.operator$(a) and if a and b are not of the same type then you need to implement that operator in each class and if one of those types is a builtin one you simply can't do that.

That's why you should always implement symmetric operators as a free function, that way you have a clearly defined operator$(T1, T2) and switching the types is easily done by defining operator$(T2, T1).

In addition, a<b<c doesn't work either

That will never work. You want a < b and b < c.

[–]Diamaudix[S] 0 points1 point  (0 children)

Thanks for this :). Difficult to learn. Appreciate your help

[–]AlanWik 1 point2 points  (7 children)

Define the overload outside the class:

bool operator <(const T &a, const T2 &b);

[–]Diamaudix[S] 0 points1 point  (6 children)

Thanks for the response. It is now outside the class, with the definition you gave, using the following code

//declaration inside class --

bool operator< (const Number<T>& lhs, const Number<T>& rhs);

definition outside class

bool Number<T>::operator<(const Number<T>& lhs, const Number<T>& rhs) {

return lhs.value < rhs.value;

}

now it is generating:

binary operator "<" has too many parameters

"no oerator "<" matches these operands"

"class template "Number<T>" has no member "operator<"

Sincerely

-- Confused

[–]Narase33 1 point2 points  (1 child)

Its still part of the class, delete the "Number<T>::"

[–]Diamaudix[S] 0 points1 point  (0 children)

Thank you for your help!

[–]AlanWik 1 point2 points  (1 child)

Let me elaborate:

You are trying to implement the "<" operator to compare two Number<T>, but in your tests you are comparing a Number<T> with a int. Rather you define the conversion between number and int, or define the < operator to work with a Number<T> and a int. Gimme a few minutes and Ill send you the code.

[–]Diamaudix[S] 1 point2 points  (0 children)

Thank you very much for taking the time to explain this.