轩辕传奇 怪物等级:重载I/O运算符问题,谢谢大虾们~~

来源:百度文库 编辑:高校问答 时间:2024/05/09 08:10:23
#include<iostream>
#include<string>
using namespace std;
class ty
{
public:
ty(int,char*);
~ty(){};
friend ostream& operator << (ostream&,ty );
private:
int m;
char*n;
};
ostream& operator << (ostream& out,ty a)
{
out << a.m<<endl<<a.n<<endl;
return out;
}
ty::ty(int x,char*y)
{
m=x;
strcpy(n,y);
}

void main()
{
ty w(710,"tianyang");
cout<<w;
}

程序提示说不能访问M.N,为什么也??
友元函数可以访问私有数据斗嘛~~~~

#include<iostream>
#include<string>
using namespace std;
class ty
{
public:
ty(int,char*);
~ty(){};
friend ostream& operator << (ostream&,ty );
private:
int m;
char*n;
};
ostream& operator << (ostream& out,ty a)
{
out << a.m<<endl<<a.n<<endl;
return out;
}
ty::ty(int x,char*y)
{
m=x;
strcpy(n,y);
}

void main()
{
ty w(710,"tianyang");
cout<<w;
}

程序提示说不能访问M.N,为什么也??
友元函数可以访问私有数据斗嘛~~~~