|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是一个对"<<"操作符进行重载,使其可以输出自定义的类中的元素的简单代码。 这段代码我有俩问题:
#include <iostream>
#include <string>
using namespace std;
class dio
{
public:
dio(int a,int b)
{
real=a,imag=b;
}
friend ostream &operator<<(ostream &os,dio i); //第一个问题在这,在这里我已经声明了友缘,但是还是报错说没权限访问private里面的元素。把dio类的“private:”去掉之后运行没问题。
private:
int real;
int imag;
};
ostream &operator<<(ostream &os,dio i) //第二个问题在这,这里为什么要加两个取地址符“&”呢?
{
os<<i.real<<','<<i.imag<<endl;
return os;
}
int main()
{
dio jojo(1,3);
cout<<jojo;
return 0;
}
这是vc6的报错信息:
E:\C语言练习\liani73\73.cpp(20) : error C2248: 'real' : cannot access protected member declared in class 'dio'
E:\C语言练习\liani73\73.cpp(15) : see declaration of 'real'
E:\C语言练习\liani73\73.cpp(20) : error C2248: 'imag' : cannot access protected member declared in class 'dio'
E:\C语言练习\liani73\73.cpp(16) : see declaration of 'imag'
E:\C语言练习\liani73\73.cpp(27) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.
|
|