S
Starx
I was wondering if someone could help me get around this problem I'm
having. I'm writing a fraction class and would like to overload the
binary arithmetic operators so that when any numerical data type (int,
double, long double, etc.) is used with a fraction it is first
converted to a fraction and then added, the result being a fraction.
So I started with this:
template <class numericType>
friend fraction operator +(const fraction& lhs, const numericType&
rhs);
template <>
friend fraction operator +(const fraction& lhs, const fraction& rhs);
The first function was simply:
{ return lhs + fraction(rhs); }
The second function was a template specialization that did the math
when adding two fractions. This worked great, as long as a fraction
was the first argument I could add it with any other data type that I
had a constructor for. But I also want addition to be commutative so
that "fraction + int" and "int + fraction" do the same thing. To get
that I added this function:
template <class numericType>
friend fraction operator +(const numericType& lhs, const fraction&
rhs);
Which did the same thing as the other function, converted explicitly to
a fraction and then added, relying on the template specialization to
add fractions. But now I get this error:
error C2667: '+' : none of 2 overload have a best conversion
How can I get rid of this? I just want it so that given:
fraction x, y;
int z;
these statements are valid and error free:
x + y;
x + z;
z + x;
I'm running VC++ 6.0
having. I'm writing a fraction class and would like to overload the
binary arithmetic operators so that when any numerical data type (int,
double, long double, etc.) is used with a fraction it is first
converted to a fraction and then added, the result being a fraction.
So I started with this:
template <class numericType>
friend fraction operator +(const fraction& lhs, const numericType&
rhs);
template <>
friend fraction operator +(const fraction& lhs, const fraction& rhs);
The first function was simply:
{ return lhs + fraction(rhs); }
The second function was a template specialization that did the math
when adding two fractions. This worked great, as long as a fraction
was the first argument I could add it with any other data type that I
had a constructor for. But I also want addition to be commutative so
that "fraction + int" and "int + fraction" do the same thing. To get
that I added this function:
template <class numericType>
friend fraction operator +(const numericType& lhs, const fraction&
rhs);
Which did the same thing as the other function, converted explicitly to
a fraction and then added, relying on the template specialization to
add fractions. But now I get this error:
error C2667: '+' : none of 2 overload have a best conversion
How can I get rid of this? I just want it so that given:
fraction x, y;
int z;
these statements are valid and error free:
x + y;
x + z;
z + x;
I'm running VC++ 6.0