一个简单的问题(请大虾帮忙)

来源:百度知道 编辑:UC知道 时间:2024/05/29 11:35:39
#include<iostream>
using namespace std;

class Complex
{
public:
Complex(int real=0,int imag=0):m_real(real),m_imag(imag)
{}
friend ostream& operator<<(ostream &out,const Complex &c);
private:
int m_real;
int m_imag;
};

ostream& operator<<(ostream &out,const Complex &c)
{
out<<c.m_real<<" "<<c.m_imag;
return out;
}

void main()
{
Complex c(5,2);
cout<<c<<endl;
}

上述程序,在vc6.0环境下编译会出现以下的错误?
error C2248: 'm_real' : cannot access private member declared in class 'Complex'
see declaration of 'm_real'
'm_imag' : cannot access private member declared in class 'Complex'
see declaration of 'm_imag'
'operator <<' is ambiguous
很是郁闷!!
第二个方案可以,可以解释一下嘛?
我觉得第一个回答不好,虽然是私有的,可是我已经申明为友元了。

恩,我也认为是编译

把头文件改成#include<iostream.h>
去掉using namespace std;
试一下

成员变量申明为private, 那就只有该类的成员函数才能使用, 外面是不能直接调用的

ps: 恩, 开始没看代码
你这个应该是编译器的问题, 如果是vc6的话请打上最新的patch

貌似没错误。我运行了下,很正常啊!