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:
Sorry for my bad English~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; }



LinkBack URL
About LinkBacks



