i have a c++ class to handle fractions and i want it to allow conversion to double,
i have something like that :
class fraction
{
double n,d;
public:
fraction(double _n, double _d) {n = _n; d = _d;}
//some functions
double todouble() {return n/d;}
};
fraction frac(1,2);
double dbl = frac.todouble();
which works fine, but i want to overload the assignment operator so i can go directly with :
double dbl = frac;
i tried to add this :
double double::operator =(double& dbl, fraction& frac) {return dbl = frac.n / frac.d;}
which resulted in the following compilation error :
error: ‘double fraction::operator=(double&, fraction&)’ must take exactly one argument
what am i doing wrong?
operator double() const { return todouble(); }
It must still be a member function though. – Burtondouble
is not a class and does not have members. – Helmicktodouble
) are slightly more verbose, but on the other hand they only occur exactly where you want them. – Ella