# Help with Cartesian class

• April 6th, 2014, 12:03 PM
cootoncandyliz
Help with Cartesian class
I am trying to make a program with a Cartesian class that allows the user to enter 2 coordinates and displays these coordinates. When I try to compile it, I get a message saying the x and y in x=c and y=d don't name a type. How can I fix this? Also, how would I go about inserting an assignment function that assigns the values of coord1 to coord2?
Code:

```#include <iostream> #include <istream> #include <ostream> using namespace std; class Cartesian { private: double x; double y; public: Cartesian( double= 0, double= 0); friend istream& operator>>(istream&, Cartesian&); friend ostream& operator<<(ostream&, const Cartesian&); double c; double d; x=c; y=d; }; Cartesian::Cartesian(double a, double b) {     x=a;     y=b; } istream& operator>>( istream& in, Cartesian& num) {         in >> num.x;         in >> num.y;     return in; } ostream& operator<<( ostream& out, const Cartesian& num) {         cout << "(" << num.x << ", " << num.y << ")" << endl; return out; } int main() {         Cartesian coord1, coord2;     cout << "Please enter the first x-coordinate: ";         cin >> coord1.c;         cout << "Please enter the first y-coordinate: ";         cin >> coord1.d;         cout << "Please enter the second x-coordinate: ";         cin >> coord2.c;         cout << "Please enter the second y-coordinate: ";         cin >> coord2.d;                 cout << coord1;         cout << coord2;                         return 0; }```
• April 6th, 2014, 12:50 PM
2kaud
Re: Help with Cartesian class
Code:

```double c; double d; x=c; y=d;```
Just remove these lines as c and d aren't used. You can't do an assignment when the compiler expects a definition/declaration.
• April 6th, 2014, 02:00 PM
cootoncandyliz
Re: Help with Cartesian class
Okay so I did that, but now I get these errors
prog.cpp: In function ‘int main()’:
prog.cpp:45:16: error: ‘class Cartesian’ has no member named ‘c’
cin >> coord1.c;
^
prog.cpp:47:16: error: ‘class Cartesian’ has no member named ‘d’
cin >> coord1.d;
^
prog.cpp:49:16: error: ‘class Cartesian’ has no member named ‘c’
cin >> coord2.c;
^
prog.cpp:51:16: error: ‘class Cartesian’ has no member named ‘d’
cin >> coord2.d;
^

Here is my current code
Code:

```#include <iostream> #include <istream> #include <ostream> using namespace std; class Cartesian { private: double x; double y; public: Cartesian( double= 0, double= 0); friend istream& operator>>(istream&, Cartesian&); friend ostream& operator<<(ostream&, const Cartesian&); }; Cartesian::Cartesian(double a, double b) {     x=a;     y=b; } istream& operator>>( istream& in, Cartesian& num) {         in >> num.x;         in >> num.y;     return in; } ostream& operator<<( ostream& out, const Cartesian& num) {         cout << "(" << num.x << ", " << num.y << ")" << endl; return out; } int main() {         Cartesian coord1, coord2;     cout << "Please enter the first x-coordinate: ";         cin >> coord1.c;         cout << "Please enter the first y-coordinate: ";         cin >> coord1.d;         cout << "Please enter the second x-coordinate: ";         cin >> coord2.c;         cout << "Please enter the second y-coordinate: ";         cin >> coord2.d;                 cout << coord1;         cout << coord2;                         return 0; }```
• April 6th, 2014, 02:10 PM
cootoncandyliz
Re: Help with Cartesian class
Okay so I made some more changes to my code and it runs now but it doesn't allow the user to enter coordinates. What am I doing wrong?
Code:

```#include <iostream> #include <istream> #include <ostream> using namespace std; class Cartesian { private: double x; double y; public: Cartesian( double= 0, double= 0); friend istream& operator>>(istream&, Cartesian&); friend ostream& operator<<(ostream&, const Cartesian&); } ; Cartesian::Cartesian(double a, double b) {     x=a;     y=b; } istream& operator>>( istream& in, Cartesian& num) {         in >> num.x;         in >> num.y;     return in; } ostream& operator<<( ostream& out, const Cartesian& num) {         cout << "(" << num.x << ", " << num.y << ")" << endl; return out; } int main() {         double x1, x2, y1, y2;         Cartesian coord1(x1, y1), coord2(x2, y2);         cout << "Please enter the first coordinates: ";         cin >> coord1;         cout << "Please enter the second coordinates: ";         cin >> coord2;         cout << coord1;         cout << coord2;                         return 0; }```
• April 6th, 2014, 02:25 PM
VictorN
Re: Help with Cartesian class
Well it's time to learn the debugger and use it to know what where and why in your code goes wrong.
• April 6th, 2014, 02:59 PM
cootoncandyliz
Re: Help with Cartesian class
What type of debugger are we talking about?
• April 6th, 2014, 03:46 PM
cootoncandyliz
Re: Help with Cartesian class
Also, what is the syntax for writing a memberwise assignment function within a class?
• April 6th, 2014, 04:12 PM
2kaud
Re: Help with Cartesian class
You are trying to make things more complicated than they are.

Code:

```#include <iostream> using namespace std; class Cartesian { private:         double x;         double y; public:         Cartesian( double a = 0, double b = 0) : x(a), y(b){}         friend istream& operator>>(istream&, Cartesian&);         friend ostream& operator<<(ostream&, const Cartesian&); }; istream& operator>>(istream& in, Cartesian& num) {         cin >> num.x >> num.y;         return in; } ostream& operator<<( ostream& out, const Cartesian& num) {         cout << "(" << num.x << ", " << num.y << ")" << endl;         return out; } int main() { Cartesian        coord1,                 coord2,                 coord3;         cout << "Please enter the first coordinates: ";         cin >> coord1;         cout << "Please enter the second coordinates: ";         cin >> coord2;         coord3 = coord1;         cout << coord1;         cout << coord2;         cout << coord3;                 return 0; }```
• April 6th, 2014, 04:19 PM
2kaud
Re: Help with Cartesian class
The declaration for an addition function for the class would be
Code:

`Cartesian operator+(const Cartesian& rh);`
This would allow two classes of type Cartesian to be added. ie
Code:

`    coord3 = coord1 + coord2;`
- although the body of the definition will need to be written! :)

This overloads the operator + to be defined for the class Cartesian. Similary, you could overload the operators +=, -=, -, * etc to perform these operations for the Cartesian class.
• April 6th, 2014, 04:33 PM
2kaud
Re: Help with Cartesian class
Quote:

Originally Posted by cootoncandyliz
Also, what is the syntax for writing a memberwise assignment function within a class?

With this class as it currently is, you don't need to provide either an assignment or a copy constructor as the default ones provided by the compiler are sufficient because no dynamic storage is involved.

See