0

Code

Vector.h

friend Vector& operator* (const double &factor, const Vector &v); friend Vector& operator* (const Vector &v,const double &factor); 

Vector.cpp

Vector& operator *(const double &factor, Vector &v){ v.x=v.x*factor; v.y=v.y*factor; return v; } Vector& operator* (const Vector &v,const double &factor){ return factor * v; } 

And for some readon i get the error at my Vector.cpp file, Vector& operator* (const Vector &v,const double &factor) -function

undefined reference to `operator*(double const&, Vector const&)'

What am i doing wrong... ?

1
  • 3
    Your declaration and definition function signatures differ! Commented May 10, 2015 at 16:47

1 Answer 1

1

As mentioned in my comment, the error occurs, because your function declaration and definition signatures differ.

Anyway with a free standing operator function one would expect to get a new Vector instance, instead of getting a reference to the one passed.

Declare / define your operator functions as follows:

friend Vector operator* (const Vector &v,double factor); friend Vector operator* (double factor, const Vector &v); Vector operator *(double factor, const Vector &v) { Vector result = v; result.x=result.x*factor; result.y=result.y*factor; return result; } Vector operator* (const Vector &v,double factor) { return factor * v; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.