求解!!!两个类都定义转换为另一个类的类型转换函数。请看代码,怎么改

disney_land 2014-04-27 02:38:58
#include<iostream>
#include<cmath>
using namespace std;
class Cpolar;
class Ccomplex
{
private:
double real;
double image;
public:
friend class Cpolar;
Ccomplex(double real=0,double image=0)
{
this->real=real;
this->image=image;
}
operator Cpolar()
{
return Cpolar(sqrt(real*real+image*image),atan(image/real));
}

};

class Cpolar
{
private:
double r;
double angle;
public:
Cpolar(double r,double angle)
{
this->r=r;
this->angle=angle;
}
operator Ccomplex()
{
return Ccomplex(r*cos(angle),r*sin(angle));
}
};
...全文
131 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ri_aje 2014-04-28
  • 打赏
  • 举报
回复
成员函数定义写在 cpp 里面就好了。
lm_whales 2014-04-28
  • 打赏
  • 举报
回复
operator = 试试
taodm 2014-04-27
  • 打赏
  • 举报
回复
根本就是没事不能使用转换运算符的。 还循环,那就完全算瞎用了。
mujiok2003 2014-04-27
  • 打赏
  • 举报
回复
引用 3 楼 u013192680 的回复:
不过那个是转换类型函数啊,如果换成
operator Cpolar * () 
意思就是转换成Cpolar指针了啊
循环依赖可以这样打破,我没有想到更好的办法。
disney_land 2014-04-27
  • 打赏
  • 举报
回复
引用 1 楼 mujiok2003 的回复:
你这是一个循环依赖, 把其中一个改成指针试试
#include<iostream>
#include<cmath>
using namespace std;
class Cpolar;
class Ccomplex
{
private:
    double real;
    double image;
public:
    friend class Cpolar;
    Ccomplex(double real=0,double image=0)
    {
        this->real=real;
        this->image=image;
    }
    operator Cpolar* ();
};

class Cpolar
{
private:
    double r;
    double angle;
public:
    Cpolar(double r,double angle)
    {
        this->r=r;
        this->angle=angle;
    }
    operator Ccomplex()
    {
        return Ccomplex(r*cos(angle),r*sin(angle));
    }
};

   Ccomplex:: operator Cpolar* ()
   { 
             //....
   }
引用 3 楼 u013192680 的回复:
不过那个是转换类型函数啊,如果换成
operator Cpolar * () 
意思就是转换成Cpolar指针了啊
求教
disney_land 2014-04-27
  • 打赏
  • 举报
回复
不过那个是转换类型函数啊,如果换成
operator Cpolar * () 
意思就是转换成Cpolar指针了啊
罗博士 2014-04-27
  • 打赏
  • 举报
回复
写成两个头文件和两个cpp文件试试。
mujiok2003 2014-04-27
  • 打赏
  • 举报
回复
你这是一个循环依赖, 把其中一个改成指针试试
#include<iostream>
#include<cmath>
using namespace std;
class Cpolar;
class Ccomplex
{
private:
    double real;
    double image;
public:
    friend class Cpolar;
    Ccomplex(double real=0,double image=0)
    {
        this->real=real;
        this->image=image;
    }
    operator Cpolar* ();
};

class Cpolar
{
private:
    double r;
    double angle;
public:
    Cpolar(double r,double angle)
    {
        this->r=r;
        this->angle=angle;
    }
    operator Ccomplex()
    {
        return Ccomplex(r*cos(angle),r*sin(angle));
    }
};

   Ccomplex:: operator Cpolar* ()
   { 
             //....
   }

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧