-
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~
-
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.
-
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).
-
Alright, thanks a a lot.
i will try to fix it and see it works or not~
Thanks again~