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