I have this simple program which converts Cartesian coordinates to Polar coordinates. I want to convert vice-versa as well. So I tried to use typecasting operator with the Cartesian class by forward-declaring it.
#include<iostream>
#include<math.h>
using namespace std;
class Cartesian;
class Polar{
double dist,angle;
public:
Polar(double a,double b): dist(a),angle(b){}
Polar(){ dist=0; angle=0; } //In case no parameters provided, this default constructor will be called
operator Cartesian(){
double x=dist*cos((angle/180)*M_PI);
double y=dist*sin((angle/180)*M_PI);
return Cartesian(x,y);
}
friend double getDATA(Polar,int);
};
double getDATA(Polar a,int opt=1){
if(opt==1) return a.dist;
else return a.angle;
}
class Cartesian{
double x,y;
public: Cartesian(){ x=0; y=0; }
Cartesian(double a,double b): x(a),y(b){}
operator Polar(){ //NOTICE:- No return type is mentioned
double distance= sqrt(x*x+y*y);
double angle= atan(y/x);
angle=(angle*180)/M_PI;
return Polar(distance,angle); //Note that new keyword is used to return pointers (allocate memory in the heap), don't use it here
}
};
int main(){
double x,y;
cout<<"Enter the X and Y coordinates= "; cin>>x>>y;
Cartesian c(x,y);
Polar p=c;
cout<<"Distance= "<<getDATA(p)<<endl;
cout<<"Gradient= "<<getDATA(p,3)<<endl;
return 0;
}
But on compiling, it gives me these 2 errors:
- return type "class Cartesian" is incomplete
- invalid use of incomplete type "class Cartesian"
Is there a workaround for this? I really want these 2 classes to be able to convert from one another.
Aucun commentaire:
Enregistrer un commentaire