标题:[求助]转换构造函数能实现这个功能么
取消只看楼主
hitlfy
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-9-8
 问题点数:0 回复次数:1 
[求助]转换构造函数能实现这个功能么
我在VC++6.0编译系统下运行这个C++程序,显示在c3=c1+2.5这条语句有错误,但我已经定义
转换构造函数了,2.5应该转为Complex(2.5),请大侠指点,不胜感激
#include <iostream.h>
class Complex
{
public:
Complex()
{
real=0;
image=0;
}
Complex(double r)
{
real=r;
image=0;
}
Complex(double r,double i)
{
real=r;
image=i;
}
friend Complex operator + (Complex &c1,Complex &c2);
void display();
private:
double real;
double image;
};
void Complex::display()
{
cout<<real<<"i"<<image<<endl;
}
Complex operator + (Complex &c1,Complex &c2)
{
return Complex(c1.real+c2.real,c1.image+c2.image);
}
int main()
{
Complex c1(3,4),c2(4,8),c3;
c3=c1+2.5;
c3.display();
return 0;
}
搜索更多相关主题的帖子: 函数 构造 
2006-12-19 07:58
hitlfy
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-9-8
得分:0 

谢谢大狭,这个知识点我还真的不知道

2006-12-20 07:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-110450-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.085053 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved