-1

Im trying to run a program with templates using operator < ,> methods, im getting a compiler error telling me "instantiated from here" and cannot convert Temps<double>' todouble' in return ,,The problem starts when i call the operator function Heres the code..

 template <class T> class Temps { private: T a; public: Temps() { } Temps(T b) { a=b; } T operator<(Temps c) { if (a < c.a) { return *this; } return c; } T operator>(Temps c) { if (a > c.a) { return *this; } return c; } }; int main() { double d1 = -9.002515,d2=98.321,d3=1.024; Temps<double>mag(d1); Temps<double>lag(d3); Temps<double>tag; tag=mag < lag; system("pause"); return 0; } 
3
  • 4
    I wouldn't use < to find the max. < should return bool. Commented Nov 18, 2011 at 0:55
  • 1
    That must be the weirdest semantics for a < operator I've ever seen. Commented Nov 18, 2011 at 1:26
  • Please don't do this. This kind of abuse of operator overloading is exactly the reason why operator overloading has such a bad reputation in some circles. Would it be so bad to have a max and a min unction for this instead of an operator which the casual reader of your code expects to do something completely different from what it does? Commented Nov 18, 2011 at 1:57

1 Answer 1

6

Your < and > functions return a T, but you are trying to return a Temps<T>. What you probably want to return is either a or c.a. But the normal semantics of < and > is to return a bool, so you may want to return a < c.a for <:

bool operator <(Temps c) { return a < c.a; } 

Similar for >.

Sign up to request clarification or add additional context in comments.

1 Comment

@drwilcox maybe tell OP to return return a instead of this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.