Thread: Need help with overloaded * operator

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    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
    Last edited by orikon; 09-23-2006 at 11:55 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM