2 class, stupid problem

Hi guys,

Have a really stupid problem, and I am not sure how to get around it. Making two classes, in order to convert between Cartesian and Polar coordinates. However, the order of class placement matters and I am unable (no matter whether I place Polar first or Cartesian first) to "see" the second class in the function "Polar::operator Cartesian()."

Of course, if I type Cartesian class and then Polar class, I will be unable to see "Cartesian:: operator Polar()."

Can someone please help me get around this problem, the book I am using does not address the issue. Also, I have not dealt with namespaces so there should be I think a pretty simple remedy.

Thanks,

Mike


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const int PHI=237;
const double PI=3.14159265358979323846;
const int DECIMAL=2;

class Polar
{
public:
	Polar(){}
	Polar(double r, int t):radius(r),phi(t){}
	operator Cartesian();
	void print(){cout<<"(r,"<<static_cast<char>(PHI)<<") = ("<<radius<<","<<phi<<")";}
	void input(){
		cout<<"Enter the radius: ";
		cin>>radius;
		while(radius<0){
			cout<<"Radius can't be less than zero, enter again: ";
			cin>>radius;
		}
		cout<<"Enter the degrees: ";
		cin>>phi;
	}
	Polar operator+(Polar& p){
		int temp_rad=p.radius+radius;
		int temp_phi=p.phi+phi;
		return Polar(radius, phi);
	}
	
	
	
	

	
	
private:
	double radius;
	int phi;
};

class Cartesian
{
public:
	Cartesian(){}
	Cartesian(double a, double b):x(a), y(b){}
	void print(){cout<<"("<<x<<","<<y<<")";}
	void input(){
		cout<<"Enter the x co-ordinate: ";
		cin>>x;cout<<endl;
		cout<<"Enter the y co-ordinate: ";
		cin>>y;
	}
	operator Polar();
	Cartesian operator+(Cartesian& c){
		double temp_x=c.x+x;
		double temp_y=c.y+y;
		return Cartesian(temp_x, temp_y);
	}
private:
	double x, y;
};


int main()
{
	char choice;
	Polar polar1(4.1,45), polar2(2.3,135);
	Cartesian cart1(4,4), cart2(2,1), add;
	do
	{
		cout<<"Polar co-ordinate 1: ";
		polar1.print();
		cout<<endl;
		cout<<"Polar co-ordinate 2: ";
		polar2.print();
		cout<<endl;
	
		Polar t=cart1;
		t.print();


		cout<<"\n\nAgain (y/n): ";
		cin>>choice;
	}while(choice=='y');

	return 0;
	_getch();
}

Cartesian::operator Polar()//convert cartesian to polar
{
	double rad=pow(x,2)+pow(y,2);
	rad=sqrt(rad);
	double angle=atan(y/x)*180/PI;
	return Polar(rad,angle);
}
Polar::operator Cartesian()
{
	//problem here
}
Your problem is a result of having all of your code in the same file. Using a forward declaration of Cartesian will fix this.
1
2
3
4
5
6
7
const int DECIMAL=2;

class Cartesian;  // forward declaration

class Polar
{
public:


The most correct fix however would be to split these into separate .h and .cpp files.
Fantastic! Thanks a lot!

I have not reached in my book separate .h and .cpp files, but it's coming up. What you suggested will do for now.

Again, thanks.
Topic archived. No new replies allowed.