### help with coordinate adding program(classes)

hello i am pretty new to coding and am having a problem with this code. it's supposed to add two coordinates and is supposed to teach me about overloading operators. any help would be greatly appreciated
every time i compile i get this

 ```main.cpp: In member function `Coordinate Coordinate::operator=(const Coordinate&) const': main.cpp:26: error: invalid use of `class Coordinate' main.cpp:27: error: invalid use of `class Coordinate' main.cpp:28: error: invalid use of `class Coordinate'```

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697`` ``````#include #include "Coordinate.h" using namespace std; Coordinate::Coordinate (double, double, double) { double x = 0.0; double y = 0.0; double z = 0.0; } Coordinate Coordinate::operator+(const Coordinate & a)const { return Coordinate(x + a.x , y + a.y, z + a.z); } Coordinate Coordinate::operator-(const Coordinate& a)const { return Coordinate(x - a.x, y - a.y, z - a.z); } Coordinate Coordinate::operator*(const Coordinate& a)const { return Coordinate(x * a.x, y * a.y, z * a.z); } Coordinate Coordinate::operator=(const Coordinate& a)const { x = a.Coordinate; y = a.Coordinate; z = a.Coordinate; return *this; } bool Coordinate::operator==(const Coordinate& a)const { return true; } bool Coordinate::operator!=(const Coordinate& a)const { return true; // can return (!(*this)==a) } ostream &operator<<(ostream &output, const Coordinate& a) { output << endl; return output; } istream &operator>>(istream &input, Coordinate& a) { return input; } #include using namespace std; #ifndef COORDINATE_H #define COORDINATE_H class Coordinate { private: double x, y, z; public: friend ostream &operator<<( ostream &, const Coordinate & ); friend istream &operator>>( istream &, Coordinate &); Coordinate (double a = 0.0, double b = 0.0, double c= 0.0); Coordinate operator+(const Coordinate & a) const; Coordinate operator-(const Coordinate&) const; Coordinate operator*(const Coordinate&) const; Coordinate operator=(const Coordinate&) const; bool operator==(const Coordinate&) const; bool operator!=(const Coordinate&) const; }; #endif #include "Coordinate.h" #include using namespace std; int main() { Coordinate a, b( 4, 8, 16 ), c( 3, 1, -2 ), k; cout << "Enter a graphical coordinate in the form: (x, y, z)\n? "; cin >> k; a = b + c; cout << "\na = b + c:\n" << a << " = " << b << " + " << c << '\n'; a = b - c; cout << "\na = b - c:\n" << a << " = " << b << " - " << c << '\n'; a = b * c; cout << "\na = b * c:\n" << a << " = " << b << " * " << c << "\n\n"; if ( a != k ) { cout << a << " != " << k << '\n'; } cout << '\n'; a = k; if ( a == k ) { cout << a << " == " << k << '\n'; } }``````

You have some issues on lines 26-28.
am i not writing it correctly or is it that i am completely off on overloading the assignment operator
You aren't writing it correctly. What does `a.Coordinate` mean?
i thought that was how you initialized it to work. Like the a was used to call it
 ``1234567`` `````` Coordinate Coordinate::operator=(const Coordinate& a)const //<---- you are promising to keep all members constant! { x = a.Coordinate; //error line y = a.Coordinate; //error line z = a.Coordinate; //error line return *this; }``````

You can remove the `const` or the error lines marked above.
I'd suggest removing the `const`, because it wouldn't make much sense to make an assignment operator promise not to change anything.
Last edited on
Topic archived. No new replies allowed.