标题:这个错误怎么改
只看楼主
W649897
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2006-11-26
 问题点数:0 回复次数:8 
这个错误怎么改

//重载流插入运算符和流提取运算符
//用重载的《进行输出
#include<iostream>

using namespace std;

class Complex
{public:
Complex(){real=0;image=0;}
Complex(int i,int j){real=i;image=j;}
Complex operator +(Complex&c1);
friend ostream& operator<<(ostream&,Complex&c);
void display(){cout<<real<<", "<<image<<endl;
private:
int real;
int image;
}
Complex Complex::operator +(Complex&c1)
{
return Complex(real+c1.real,c1.image+image);
}
ostream& operator << (ostream&output,Complex&c)
{
output<<"( "<<c.real<<" + "<<c.image<<"i)"<<endl;
return output;
}
int main()
{
Complex c2(2,4),c3(3,6),c4;
c4=c2+c3;
cout<<c4;
return 0;
}

--------------------Configuration: chapter10 - Win32 Debug--------------------
Compiling...
pr4.cpp
E:\temp\chapter10\pr4.cpp(19) : error C2535: 'class Complex __thiscall Complex::operator +(class Complex &)' : member function already defined or declared
E:\temp\chapter10\pr4.cpp(11) : see declaration of '+'
E:\temp\chapter10\pr4.cpp(23) : error C2804: binary 'operator <<' has too many parameters
E:\temp\chapter10\pr4.cpp(23) : error C2333: '<<' : error in function declaration; skipping function body
E:\temp\chapter10\pr4.cpp(35) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

chapter10.exe - 4 error(s), 0 warning(s)
本人刚学C++,多多指教了

搜索更多相关主题的帖子: Complex int operator real image 
2006-12-02 11:30
neverTheSame
Rank: 3Rank: 3
来 自:江西农业大学
等 级:新手上路
威 望:9
帖 子:1511
专家分:0
注 册:2006-11-24
得分:0 

#include<iostream>

using namespace std;

class Complex
{public:
Complex(){real=0;image=0;}
Complex(int i,int j){real=i;image=j;}
Complex operator +(Complex&c1);
friend ostream& operator<<(ostream&,Complex&c);
void display(){cout<<real<<", "<<image<<endl; }//少了"}"
private:
int real;
int image;
} ;//少了";"
Complex Complex::operator +(Complex&c1)
{
return Complex(real+c1.real,c1.image+image);
}
ostream& operator << (ostream&output,Complex&c)
{
output<<"( "<<c.real<<" + "<<c.image<<"i)"<<endl;
return output;
}
int main()
{
Complex c2(2,4),c3(3,6),c4;
c4=c2+c3;
cout<<c4;
return 0;
}


wap酷禾网(http://wap.),提供免费的、优质的、快捷的wap资源下载服务。
2006-12-02 21:47
W649897
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2006-11-26
得分:0 

运行时有以下错误
--------------------Configuration: ch - Win32 Debug--------------------
Compiling...
c.cpp
e:\visual c++ 6.0 cn\msdev98\myprojects\ch\c.cpp(22) : error C2248: 'real' : cannot access private member declared in class 'Complex'
e:\visual c++ 6.0 cn\msdev98\myprojects\ch\c.cpp(13) : see declaration of 'real'
e:\visual c++ 6.0 cn\msdev98\myprojects\ch\c.cpp(22) : error C2248: 'image' : cannot access private member declared in class 'Complex'
e:\visual c++ 6.0 cn\msdev98\myprojects\ch\c.cpp(14) : see declaration of 'image'
e:\visual c++ 6.0 cn\msdev98\myprojects\ch\c.cpp(29) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

ch.exe - 3 error(s), 0 warning(s)

2006-12-08 19:04
W649897
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2006-11-26
得分:0 
你改的还是不对
2006-12-08 19:05
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
得分:0 

这样用 #include<iostream.h>


天行健,君子以自强不息!!QQ:68660681
2006-12-08 20:25
W649897
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2006-11-26
得分:0 
为什么呀
2006-12-09 11:33
zzydeai
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-11-27
得分:0 
Complex(){real=0;image=0;}
Complex(int i,int j){real=i;image=j;}
Complex operator +(Complex&c1);
为什么写三个重载函数我就不懂了
Complex Complex::operator +(Complex&c1)
这里是不是一个Complex就合适了

试着做一个顶级黑客或者伟大的企业家
2006-12-11 16:16
W649897
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2006-11-26
得分:0 
不是呀,第一个是无参构造函数,第二个是有参构造函数;第三个是运算符重载,不一样的
2006-12-11 20:21
smartwind
Rank: 1
等 级:新手上路
威 望:1
帖 子:277
专家分:0
注 册:2006-11-13
得分:0 
2楼的程序可以正确运行呀,你那里不行吗?

2006-12-15 21:47



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




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

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