关于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编译器出问题! 展开
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编译器出问题! 展开
展开全部
#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定义友元函数时不要在操作符前加空格
我这个程序也不能运行吗?应该不会吧。。。
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定义友元函数时不要在操作符前加空格
我这个程序也不能运行吗?应该不会吧。。。
展开全部
protected:
友元不能访问保护 成员
改为private
友元不能访问保护 成员
改为private
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
重载运算符有岐义。
in>>应该是cin>>吧。
in>>应该是cin>>吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询