overloading - C++ no match for operator* -
overloading - C++ no match for operator* -
i want overload operator:
static vector operator*(float s, vector right){ vector result(right.getx()*s, right.gety()*s, right.getz()*s); homecoming result; }
when want utilize it:
vector a(0,1,5) vector v(4*a);
i got:
error: no match 'operator*' (operand types 'int' , 'vector')
what wrong?
you need declare (within class declaration) operator* non-static non-member , friend like:
friend vector operator*(float s, vector right);
and define (outside class declaration):
vector operator*(float s, vector right){ homecoming vector(right.getx()*s, right.gety()*s, right.getz()*s); }
operator * used binary operator here. if create member, first argument implicitly taken current object (on * applies, example, if utilize x * y, operator * applies on x member). however, non-member both arguments can non-vector type , can converted vector if needed. work if create non-member non-friend , static (tobe defined outside class declaration)
c++ overloading operator-keyword
Comments
Post a Comment