2015-08-25 142 views
1

我需要將笛卡爾座標轉換爲極座標。在類型轉換爲polar時出現的錯誤是「Polar之前的預期類型說明符」。請幫助我。我讀了其他帖子,發現我需要指定類的類型。我試過它指定爲「笛卡爾」。我應該怎麼指定?提前Thanx。我得到其中被註釋掉運算符重載類型轉換:極座標到笛卡爾C++

#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; 
} 
+1

搜索StackOverflow以進行「前向聲明」。 –

+2

一個簡單的解決方法,無需閱讀'foward declaration'就可以在'class Cartesian'之前聲明'class Plolar'。 –

回答

3

極地類在您定義operator Polar點沒有定義區段的錯誤,所以你需要轉發聲明它與極地的定義後實施。此外,您正在訪問Polar的私人會員,因此您需要聲明笛卡爾爲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; 
} 
+0

Thanx @Maksim :)。這真的有幫助 – punitrokzz

相關問題