C++ 运算符重载类型转换:极坐标到笛卡尔c++;

C++ 运算符重载类型转换:极坐标到笛卡尔c++;,c++,C++,我需要把笛卡尔坐标转换成极坐标。我在向polar进行类型转换时遇到的错误是“polar之前的预期类型说明符”。请帮帮我。我阅读了其他帖子,发现我需要指定类类型。我试着将其指定为“笛卡尔”。如何以及我应该指定什么??提前准备好。我在注释掉的部分中得到了错误 #include<iostream> #include<cstdio> #include<cmath> using namespace std; class Cartesian { double x,

我需要把笛卡尔坐标转换成极坐标。我在向polar进行类型转换时遇到的错误是“polar之前的预期类型说明符”。请帮帮我。我阅读了其他帖子,发现我需要指定类类型。我试着将其指定为“笛卡尔”。如何以及我应该指定什么??提前准备好。我在注释掉的部分中得到了错误

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
class Cartesian
{
    double x,y;
public:
    Cartesian()
    {
        x=0.0;y=0.0;
    }
    Cartesian(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
    Cartesian(const Cartesian& p)
    {
        x=p.x;
        y=p.y;
    }
    int getX()
    {
        return x;
    }
    int getY()
    {
        return y;
    }
    double operator-(Cartesian b)
    {
        double dist;
        dist=sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));
        return dist;
    }
    /*operator Polar()
    {
        Polar temp;
        temp.r=sqrt((x*x)+(y*y));
        temp.theta=atan(y/x);
        return temp;
    }*/




};


class Polar
{
    double r,theta;
public:
    Polar()
    {
        r=0.0;theta=0.0;
    }
    Polar(int r,int theta)
    {
            this->r=r;
            this->theta=theta;
    }
    Polar(const Polar& p)
    {
        r=p.r;
        theta=p.theta;

    }
     int getR()
    {
        return r;
    }
    int getTheta()
    {
        return theta;
    }
    double operator-(Polar b)
    {
        double dist;
        dist=sqrt(r*r+(b.r)*(b.r)+2*(r)*(b.r)*cos(theta-b.theta));
        return dist;

    }

};

int operator==(Cartesian a,Cartesian b)
{
    int t1,t2;
    t1=a.getX();
    t2=b.getX();
    if(t1==t2)
    {
        t1=a.getY();
        t2=b.getY();
        if(t1==t2)
            return 1;
    }
    return 0;
}

int operator==(Polar a,Polar b)
{
    int t1,t2;
    t1=a.getR();
    t2=b.getR();
    if(t1==t2)
    {
        t1=a.getTheta();
        t2=b.getTheta();
        if(t1==t2)
            return 1;
    }
    return 0;
}

int operator==(Cartesian a,Polar b)
{

    int t1,t2,t3,t4;
    t1=a.getX();
    t2=a.getY();
    t3=b.getR();
    t4=b.getTheta();
    if((sqrt(t1*t1+t2*t2)==t3)&&(atan(t2/t1)==t4))
        return 1;
    else
        return 0;
}






int main()
{
    double temp1,temp2,temp3,temp4,distance;
    cout<<"Enter the x and y coordinates of the first point\n";
    cin>>temp1>>temp2;
    Cartesian a1(temp1,temp2);
    cout<<"Enter the x and y coordinates of the second point\n";
    cin>>temp3>>temp4;
    Cartesian b1(temp3,temp4);
    distance=a1-b1;
    cout<<distance<<"\n";
    cout<<"Enter the r and theta coordinates of the first point\n";
    cin>>temp1>>temp2;
    Polar a2(temp1,temp2);
    cout<<"Enter the r and theta coordinates of the second point\n";
    cin>>temp3>>temp4;
    Polar b2(temp3,temp4);
    distance=a2-b2;
    cout<<distance<<"\n";
    if(a1==a2)
        cout<<"True";





    return 0;
}
#包括
#包括
#包括
使用名称空间std;
类笛卡尔
{
双x,y;
公众:
笛卡尔的()
{
x=0.0;y=0.0;
}
笛卡尔(整数x,整数y)
{
这个->x=x;
这->y=y;
}
笛卡尔(常量笛卡尔&p)
{
x=p.x;
y=p.y;
}
int getX()
{
返回x;
}
int getY()
{
返回y;
}
双算子-(笛卡尔b)
{
双区;
dist=sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));
返回距离;
}
/*运算符Polar()
{
极温;
温度r=sqrt((x*x)+(y*y));
温度θ=atan(y/x);
返回温度;
}*/
};
极轴类
{
双r,θ;
公众:
极性()
{
r=0.0;θ=0.0;
}
极坐标(整数r,整数θ)
{
这个->r=r;
这->θ=θ;
}
极地(恒极地和太平洋)
{
r=p.r;
θ=p.θ;
}
int getR()
{
返回r;
}
int getTheta()
{
返回θ;
}
双运算符-(极性b)
{
双区;
dist=sqrt(r*r+(b.r)*(b.r)+2*(r)*(b.r)*cos(θ-b.θ));
返回距离;
}
};
int运算符==(笛卡尔a,笛卡尔b)
{
int t1,t2;
t1=a.getX();
t2=b.getX();
如果(t1==t2)
{
t1=a.getY();
t2=b.getY();
如果(t1==t2)
返回1;
}
返回0;
}
int运算符==(极轴a、极轴b)
{
int t1,t2;
t1=a.getR();
t2=b.getR();
如果(t1==t2)
{
t1=a.getTheta();
t2=b.getTheta();
如果(t1==t2)
返回1;
}
返回0;
}
int运算符==(笛卡尔a,极坐标b)
{
int t1、t2、t3、t4;
t1=a.getX();
t2=a.getY();
t3=b.getR();
t4=b.getTheta();
如果((sqrt(t1*t1+t2*t2)=t3)和&(atan(t2/t1)=t4))
返回1;
其他的
返回0;
}
int main()
{
双节拍1,节拍2,节拍3,节拍4,距离;
couttemp1>>temp2;
笛卡尔a1(temp1,temp2);
couttemp3>>temp4;
笛卡尔b1(temp3,temp4);
距离=a1-b1;
couttemp4;
极性b2(temp3,temp4);
距离=a2-b2;

cout在定义
操作符Polar
时未定义类Polar,因此您需要在定义Polar后向前声明并实现它。此外,您正在访问Polar的私有成员,因此您需要将Cartesian声明为Polar的朋友:

class Polar; // forward declaration

class Cartesian {
    //...

    operator Polar(); // only the declaration
};

class Polar {
    friend class Cartesian; // so that Cartesian sees private members

    //...
};

// implementation of operator Polar()
Cartesian::operator Polar() {
    Polar temp;
    temp.r=sqrt((x*x)+(y*y));
    temp.theta=atan(y/x);
    return temp;
}

搜索StackOverflow“forward declaration”。一个不必费心阅读“forward declaration”的简单修复方法是在
class Cartesian
之前声明
class Plolar
。Thanx@Maksim:)。这确实有帮助