Cartesian class

I am having difficulty completing my code for a Cartesian class. It is supposed to allow for the input and display of an object's x and y values, but I can't figure out what to put in the implementation section. Here is my code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <iostream>

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&);

Return 0;
}

Istream& operator>>( istream& in, Cartesian& num)
{
	in >> value;
//statements to parse the input stream
// referenced by the identifier variablename

return in;
}

Ostream& operator<<( ostream& out, const Cartesian& num)
{
//statements placing data on the output stream
// referenced by the identifier variablename

return out;
}


int main()
{
	Cartesian coord;
	cin >> coord;
	
	return 0;
}
Line 18: Istream& vs istream& Choose one

Line 27: Ostream& vs ostream&, again choose one

http://www.cplusplus.com/reference/ostream/ostream/ostream/
http://www.cplusplus.com/reference/istream/istream/istream/

EDIT (missed these):
1
2
Friend istream& operator>>(istream&, Cartesian&);
Friend ostream& operator<<(ostream&, const Cartesian&);

vs
1
2
friend istream& operator>>(istream&, Cartesian&);
friend ostream& operator<<(ostream&, const Cartesian&);



Return 0; vs return 0;

Again choose one and remember that only one combination works (uppercase or lowercase)

Also remember to #include <istream> #include <ostream>

line 11: Cartesian( double= 0, double= 0) {/*initialisations*/}
Last edited on
Okay so I've mostly fixed it now, but it still won't run properly. Here is what I have.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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&);

return 0;
};

istream& operator>>( istream& in, Cartesian& num)
{
    double c, d;
	num.x= c;
	num.y= d;

	in >> c >> d;

return in;
}

ostream& operator<<( ostream& out, const Cartesian& num)
{
	cout << "(" << num.x << ", " << num.y << ")" << endl;

return out;
}


int main()
{
	Cartesian coord;
	cin >> coord;
	cout << coord;
	
	return 0;
}
Last edited on
When you say it isn't running properly...can you be more specific? What error messages are you getting, what output are you getting? My guess is that any trouble you are receiving is coming from that constructor you have not declared or lack of a default constructor
It says there is an expected unqualified-id before the first return
Right, that is because that is not a function. Remove that return statement. Return statements are only meant for functions.
Okay so I fixed that but now I'm getting this error
In function `main':
main.cpp:(.text+0xf8): undefined reference to `Cartesian::Cartesian(double, double)'
collect2: error: ld returned 1 exit status

Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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&);

};

istream& operator>>( istream& in, Cartesian& num)
{
    double c, d;
	num.x= c;
	num.y= d;

	in >> c >> d;

return in;
}

ostream& operator<<( ostream& out, const Cartesian& num)
{
	cout << "(" << num.x << ", " << num.y << ")" << endl;

return out;
}

int main()
{
	Cartesian coord;
	cin >> coord;
	cout << coord;
	
	return 0;
}
Topic archived. No new replies allowed.