I have the following classes and the compiler (Microsoft Visual Studio 2012) gives me strange result since there is no problem compiling a*v
but compiling b*v
I get the following error:
"error C2678: binary '*': no operator found which takes a right-hand operator of type const double (or there is no acceptable conversion).
The compiler does not use the A::operator*()
for a*v
, but for b*v
the function operator*(U t, Vector<T> v)
is used.
So does anyone know what is going on?
template <class T>
class Vector
{
public:
Vector() { v[0] = 1; v[1] = 2; v[2] = 3; }
Vector(T a, T b, T c) { v[0] = a; v[1] = b; v[2] = c; }
T v[3];
};
template <class T, class U>
Vector<U> operator*(const T& t, const Vector<U>& v)
{
return Vector<U>(t*v.v[0], t* v.v[1], t*v.v[2])
}
class A
{
public:
Vector<double> operator *(const Vector<double>& v)
{
return Vector<double>(99.0,99.0,99.0);
}
};
class B : public A { };
void MyFct()
{
Vector<double> v;
A a;
B b;
Vector<double> test1 = a * v;
Vector<double> test2 = b * v;
printf("%f %f", test1.v[0], test2.v[0]);
}