关于C++友元函数重载问题,下面是我写的程序,可在VC6.0显示错误。

#include<iostream>usingnamespacestd;classcomplex{protected:doublereal;doubleimage;pub... #include<iostream>
using namespace std;
class complex
{
protected:
double real;
double image;
public:
complex(double r=0,double i=0):real(r),image(i){}
void set(double r,double i){real=r; image=i;}
double Getreal() { return real; }
double Getimage(){ return image; }
friend istream &operator >>(istream &in, complex &z);
friend ostream &operator <<(ostream &out,complex &z);

};
istream &operator>>(istream &in, complex &z)
{

cout<<"输入实部:"<<endl;
in>>z.real;
cout<<"输入虚部:"<<endl;
in>>z.image;

return in;
}

ostream &operator<<(ostream &out,complex &z)
{

out<<z.real<<"+"<<z.image<<"i"<<endl;

return out;
}

int main()
{
complex t1;
complex t2;

cin>>t1;
cin>>t2;
cout<<t1;
cout<<t2;

return 0;
}
/* error C2248: 'real' : cannot access protected member declared in class 'complex'
C:\Documents and Settings\Owner\My Documents\Text1.cpp(6) : see declaration of 'real'
C:\Documents and Settings\Owner\My Documents\Text1.cpp(23) : error C2248: 'image' : cannot access protected member declared in class 'complex'
C:\Documents and Settings\Owner\My Documents\Text1.cpp(7) : see declaration of 'image'
C:\Documents and Settings\Owner\My Documents\Text1.cpp(31) : error C2248: 'real' : cannot access protected member declared in class 'complex'
C:\Documents and Settings\Owner\My Documents\Text1.cpp(6) : see declaration of 'real'
C:\Documents and Settings\Owner\My Documents\Text1.cpp(31) : error C2248: 'image' : cannot access protected member declared in class 'complex'
C:\Documents and Settings\Owner\My Documents\Text1.cpp(7) : see declaration of 'image'
C:\Documents and Settings\Owner\My Documents\Text1.cpp(41) : error C2593: 'operator >>' is ambiguous
C:\Documents and Settings\Owner\My Documents\Text1.cpp(42) : error C2593: 'operator >>' is ambiguous
C:\Documents and Settings\Owner\My Documents\Text1.cpp(43) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Text1.cpp(44) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe. */
//求高手帮我看看,做很久了,不知道为什么?
估计是我的VC编译器出问题!
展开
 我来答
BTzjzxxx
2011-06-21 · TA获得超过4534个赞
知道大有可为答主
回答量:981
采纳率:0%
帮助的人:657万
展开全部
#include <iostream>
using namespace std;

class complex {
protected:
double real;
double image;
public:
complex(double r=0,double i=0):real(r),image(i){}
void set(double r,double i){real=r; image=i;}
double Getreal() {return real;}
double Getimage() {return image;}
friend istream &operator>>(istream &in, complex &z);
friend ostream &operator<<(ostream &out,complex &z);
};

istream &operator>>(istream &in, complex &z) {
cout<<"输入实部:"<<endl;
in>>z.real;
cout<<"输入虚部:"<<endl;
in>>z.image;
return in;
}

ostream &operator<<(ostream &out,complex &z) {
out<<z.real<<"+"<<z.image<<"i"<<endl;
return out;
}

int main()
{
complex t1;
complex t2;
cin>>t1;
cin>>t2;
cout<<t1;
cout<<t2;
return 0;
}

试一下,protect的问题不知道是不是VC的问题,VS下运行没有问题,你的operator定义友元函数时不要在操作符前加空格

我这个程序也不能运行吗?应该不会吧。。。
cocopark1982
2011-06-21 · TA获得超过348个赞
知道小有建树答主
回答量:556
采纳率:0%
帮助的人:492万
展开全部
protected:
友元不能访问保护 成员
改为private
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
军军106
2011-06-21 · TA获得超过453个赞
知道小有建树答主
回答量:1747
采纳率:0%
帮助的人:766万
展开全部
重载运算符有岐义。
in>>应该是cin>>吧。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
q1023413453
2011-06-21
知道答主
回答量:7
采纳率:0%
帮助的人:0
展开全部
什么的

参考资料:

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式