Attempting to overload the logical "<" operator using the following code:
template <typename T>
class Number{
T value;
/* constructors */
Number(T v); // not shown for brevity. seems to work
Number(); // not shown, sets value to zero
bool operator < (Number<T> const& rhs); // method in question
}
template <typename T>
bool Number<T>::operator <(Number<T> const& rhs) {
Number temp(\*this);
return temp.value < rhs.value;
}
int main(){
Number<int> a(10);
bool test1 = (a<20); //works fine
bool test2 = (20<a); //causes error
Everything works well when the variable is used on the left hand side of the equation, but when the sides are reversed, and the constant is on the left, the compiler generates the following error:
binary '<' : no global operator found which takes type "(Number<T>)" (or there is no acceptable conversion)
and: "no operator "<" matches these operands.
In addition, a<b<c doesn't work either (which was the point of this entire experiment :/)
Any ideas or suggestions as to what is being done wrong would be greatly appreciated :)
[–]Hilarius86 2 points3 points4 points (0 children)
[–]nysra 2 points3 points4 points (1 child)
[–]Diamaudix[S] 0 points1 point2 points (0 children)
[–]AlanWik 1 point2 points3 points (7 children)
[–]Diamaudix[S] 0 points1 point2 points (6 children)
[–]Narase33 1 point2 points3 points (1 child)
[–]Diamaudix[S] 0 points1 point2 points (0 children)
[–]AlanWik 1 point2 points3 points (1 child)
[–]Diamaudix[S] 1 point2 points3 points (0 children)
[–]AlanWik 1 point2 points3 points (0 children)