Operator Overloading

This is a discussion on Operator Overloading within the C++ Programming forums, part of the General Programming Boards category; Hi... I got this assignment on operator overloading, and i am new to this topic too. My lecture said it ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    Operator Overloading

    Hi...
    I got this assignment on operator overloading, and i am new to this topic too. My
    lecture said it is easy but i am new so it is not for me.

    So when i done with my code, the complier gives me some error on a particular
    operator that is the multiplication (*). Definitely there is something wrong with my
    code on the function which i overload this operator, so i hope all the pros here can
    help me figure it out? Million of thanks in advance~!

    here is my code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class myVector
    {
    	  int x, y, z; // Coordinates
    	  
    	  public:
    			 myVector(){x = y = z =0;}
    			 myVector(int i, int j, int k) {x =i; y = j; z = k;}
    			 myVector operator+(myVector op2); // op1 is implied
    			 myVector operator-(myVector op2); // op1 is implied
    			 myVector operator*(myVector op2); // op1 is implied
    			 myVector operator/(myVector op2); // op1 is implied
    			 myVector operator=(myVector op2); // op1 is implied
    			 
    			 myVector operator++(); // Prefix version of ++
    			 
    			 void show();	 
    	  };
    
    // Overload +
    myVector myVector::operator+(myVector op2)
    {
    	   myVector temp;
    	   
    	   temp.x = x + op2.x;
    	   temp.y = y + op2.y;
    	   temp.z = z + op2.z;
    	   
    	   return temp;
    	   }
    
    // Overload -
    myVector myVector::operator-(myVector op2)
    {
    	   myVector temp;
    	   
    	   temp.x = x - op2.x;
    	   temp.y = y - op2.y;
    	   temp.z = z - op2.z;
    	   
    	   return temp;
    	   }
    	   
    // Overload *
    myVector myVector::operator*(myVector op2)
    {
    	   myVector temp;
    	   
    	   temp.x *= op2.x;
    	   temp.y *= op2.y;
    	   temp.z *= op2.z;
    	   
    	   return *this;
    	   }
    
    // Overload Assignment
    myVector myVector::operator=(myVector op2)
    {
    	   x = op2.x;
    	   y = op2.y;
    	   z = op2.z;
    	   
    	   return *this;
    	   }
    
    // Overload the prefix version of ++
    myVector myVector::operator++()
    {
    		 x++;
    		 y++;
    		 z++;
    		 
    		 return *this;
    		 }
    
    // Show x, y , z coordinates
    void myVector::show()
    {
    	 cout << x << ", ";
    	 cout << y << ", ";
    	 cout << z << "";
    	 cout << " " << endl;
    	 }
    	  
    int main()
    {
    	int first = 0, second = 0, third = 0;
    
    	cout << " " << endl;
    	cout << "*----------------- Overloading ---------------*" << endl;
    	
    	cout << "Enter first vector, format (a,b,c): " << endl;;
    	cin >> first;
    	cin >> second;
    	cin >> third;
    	
    	myVector A(first, second, third); // Set value of Vector A
    	A.show();
    
    	cout << "Enter second vector, format (a,b,c): " << endl;;
    	cin >> first;
    	cin >> second;
    	cin >> third;
    	
    	myVector B(first, second, third); // Set value of Vector B
    	B.show();
    	
    	myVector C; // Set value of Vector C
    	
    	C = A + B;
    	cout << "Addition A+B = ";
    	C.show();
    	
    	C = A - B;
    	cout << "Subtraction A-B = ";
    	
    	
    	C = 5 * A; // <----- THIS PART HERE WHICH IS GIVING ME PROBLEMS
    			  // < ----- IT SAYS "NO MATCH FOR 'operator*' IN '5*A'
    	cout << "Scalar Multiplication 5*A = ";
    	C.show();
    
    	C = ++A;
    	cout << "Prefix ++A = ";
    	C.show();
    	
    	system("Pause");
    	return 0;
    	}
    Sorry for my bad English~

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    i will throw this in my compiler and see what shakes out, but right off the bat, there is a computational error in operator*, aside from any compilation errors - you're not multiplying a*b; you're multiplying default*b.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    There are computational errors in every operator... almost.
    The thing is that we want to, say, multiply something to with the current object and return the result. So obviously we cannot modify the original object (op1), so we need a temp. But that temp needs the values stored in op1, so you have to initialize it with *this.
    myVector temp(*this);

    Then you can proceed as usual.

    As for the error, what you do is multiply an integer with a myVector, so the compiler looks for a operator * (int, myVector), which does not exist. Your overloaded member operator * is a operator * (myVector, myVector), which is not right. You need to use operators in the global namespace, which is easy, but I do not know if you have learned this yet. If not, then I would just put the integer on the right side of the multiplication and overload an operator * (int).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    Thumbs up

    Alright, thanks a a lot.
    i will try to fix it and see it works or not~

    Thanks again~

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21