| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 516 人关注过本帖
标题:运算符重载时出现问题了
收藏  订阅  推荐  打印
zerocn
Rank: 2
等级:注册会员
帖子:124
积分:1458
注册:2006-4-11
运算符重载时出现问题了

引用:
#include <iostream>

using namespace std;

class Complex
{private:
float r,i;
public:
Complex operator + (Complex &b);
Complex();
Complex(float &,float &);
void show();
};

Complex Complex::operator +(Complex &b)
{Complex temp;
temp.r=r+b.r;
temp.i=i+b.i;
return (temp);
}

Complex::Complex()
{r=0;i=0;}
Complex::Complex(float &a, float &b)
{r=a;i=b;}
void Complex::show()
{if(r<0)cout<<r<<"-"<<i<<"i"<<endl;
else if(r=0)cout<<r<<endl;
else cout<<r<<"+"<<i<<endl;
               }

int main()
{ Complex c1(10,11),c2(12,13),c3;//编译时这里出错了
  c3=c1+c2;
  c3.show();  
  return 0;
}
这里是报错的消息
引用:
no matching function for call to `Complex::Complex(int, int)'
Complex::Complex(float&, float&)
Complex::Complex()
我想是不是创建对象是把"10"看成int形,那么我改为"10F"这样还是不行
2008-6-25 17:46
zjl138
Rank: 4
等级:高级会员
帖子:764
积分:9144
威望:1
注册:2007-11-12

Complex(float &,float &);此函数改声明为Complex(float ,float );

定义那里也改一下.

i like linux...
2008-6-25 18:16
hp521_kylin
Rank: 1
等级:新手上路
帖子:5
积分:150
来自:px
注册:2008-6-25

我编译了一下:问题是你说的那个。
但是你有没有看明白:不能把const int 转换为float 还有2楼这位仁兄说的引用问题..

c++,my goals
2008-6-25 21:51
mqh21364
Rank: 4
等级:高级会员
帖子:638
积分:6970
注册:2008-2-28

那你就写成10.0这样不就好了啊!

前不见古人,后不见来者。念天地之悠悠,独怆然而涕下。
2008-6-26 16:44
guipengee
Rank: 1
等级:新手上路
帖子:2
积分:120
注册:2008-6-26

编译器使所有的临时对象自动成为const,你的参数按引用传递,就意味着它取临时对象的地址,就有可能修改临时对象。在参数里加上const就OK。
2008-6-26 19:26
漫游者李李西
Rank: 2
等级:注册会员
帖子:106
积分:1256
注册:2007-11-11

常数认为是const,可以在申明时加上const,要不在创建对象时传入名字代替10.

2008-6-26 22:18
kongwei254
Rank: 1
等级:新手上路
帖子:38
积分:440
注册:2008-5-18

#include <iostream.h>

//using namespace std;

class Complex
{private:
float r,i;
public:
Complex operator + (Complex &b);
Complex();
Complex(float ,float );
void show();
};

Complex Complex::operator +(Complex &b)
{Complex temp;
temp.r=r+b.r;
temp.i=i+b.i;
return (temp);
}

Complex::Complex()
{r=0;i=0;}
Complex::Complex(float a, float b)
{r=a;i=b;}
void Complex::show()
{
    if(r<0)cout<<r<<"-"<<i<<"i"<<endl;
    else if(r=0)cout<<r<<endl;
    else cout<<r<<"+"<<i<<endl;
}

int main()
{ Complex c1(10,11),c2(12,13),c3;
  c3=c1+c2;
  c3.show();  
  return 0;
}



可以了
2008-6-27 21:56
雪城白鸟
Rank: 1
等级:新手上路
帖子:17
积分:246
注册:2008-3-15
我的答案

#include <iostream>
using namespace std;


class Complex
{
private:
        float RE_P;//real part
        float IM_P;//imaginary part
public:
        Complex operator + (const Complex &b);
        Complex(){RE_P=0; IM_P=0; }
        Complex(float a,float b ){RE_P=a; IM_P=b; }
        void show()const;
};

Complex Complex::operator +(const Complex &b)
{
    Complex temp;
    temp.RE_P=RE_P+b.RE_P;
    temp.IM_P=IM_P+b.IM_P;
    return temp;
}

void Complex::show()const
{
    if(IM_P==0)
      cout<<RE_P<<endl;
    else if(IM_P<0)
      cout<<RE_P<<IM_P<<"i\n";
    else
      cout<<RE_P<<"+"<<IM_P<<"i\n";
}

int main()
{ Complex c1(10,11),c2(12,13),c3;
  c3=c1+c2;
  c3.show();  
  return 0;
}
2008-6-28 16:22
雪城白鸟
Rank: 1
等级:新手上路
帖子:17
积分:246
注册:2008-3-15
kongwei254的不对

你的输出有问题,希望检查下
2008-6-28 16:23
byd913
Rank: 1
等级:新手上路
帖子:4
积分:146
注册:2008-5-2
改一下就可以了

#include <iostream>

using namespace std;

class Complex
{private:
float r,i;
public:
Complex operator + (Complex &b);
Complex();
Complex(float ,float );
void show();
};

Complex Complex::operator +(Complex &b)
{Complex temp;
temp.r=r+b.r;
temp.i=i+b.i;
return (temp);
}

Complex::Complex()
{r=0;i=0;}
Complex::Complex(float a, float b)
{r=a;i=b;}
void Complex::show()
{if(r<0)cout<<r<<"-"<<i<<"i"<<endl;
else if(r=0)cout<<r<<endl;
else cout<<r<<"+"<<i<<endl;
               }

int main()
{ Complex c1(10,11),c2(12,13),c3;//编译时这里出错了
  c3=c1+c2;
  c3.show();  
  return 0;
}
2008-7-4 14:19
共有 515 人关注过本帖
发新话题
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.056129 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved