# Need help with overloaded * operator

• 09-23-2006
orikon
Need help with overloaded * operator
I'm writing a vector class that will add subtract multiply vectors etc, but I'm having trouble with the * operator. I need to multiply a vector (a complex datatype defined by the class) by an integer, basically scalar multiplication, but I get this error when I compile: error: no match for 'operator*' in '2 * estvec' where 2 is just the integer I used for testing. I tried two ways of doing it but neither of them work. Here is the code:

The public/private declarations of the class (not complete):
Code:

```class vector         {         private:                 double magnitude;                 double direction;         public:                 vector();                                                    //Default consructor                 vector(const vector &old);                          //Copy Constructor                                                                                 //Overloaded operators                 //for first method                 friend vector operator *(const vector &left, const vector &right);                 //for second method.  Note: I only kept one or the other                 vector operator*(const vector &num);                                                                           };```
Here's the first method:
Code:

```         vector operator *(const vector &left, const vector &right)                 {                 vector product;                 product.magnitude = left.magnitude * right.magnitude;                 if (left.magnitude < 0.0)                         {                         product.direction = right.direction + 180.0;                         }                 //this is based on a 360 degree system                 return product;                 }```
Here's the second method:
Code:

```        vector vector::operator*(const vector &num)                 {                 vector result;                 result.magnitude = magnitude * num.magnitude;                 result.direction = num.direction;                 return result;                 }```
I simply used this to test the code:
Code:

```                vector prod;                 prod = 2 * estvec;        //estvec is just another vector I declared earlier                 cout << prod;              //I have a working overloaded <<  operator```
• 09-24-2006
Daved
You should make operator* a non-member function and have three versions. One that multiplies two vectors, one that has the vector on the left and an int on the right, and one that has an int on the left and the vector on the right. Your second attempt is a member function, meaning a vector is automatically on the left, and it takes another vector you named num. You want one of the variable types to be int in order to multiply by an int. You also have to make it a non-member function so the left hand side isn't forced to be a vector. In your example, 2 * estvec, the int is on the left.
• 09-24-2006
orikon
Quote:

Originally Posted by Daved
You should make operator* a non-member function and have three versions. One that multiplies two vectors, one that has the vector on the left and an int on the right, and one that has an int on the left and the vector on the right. Your second attempt is a member function, meaning a vector is automatically on the left, and it takes another vector you named num. You want one of the variable types to be int in order to multiply by an int. You also have to make it a non-member function so the left hand side isn't forced to be a vector. In your example, 2 * estvec, the int is on the left.

That makes a lot of sense, thanks so much. I've got it working now!