潍坊时代箱货汽车报价:重载I/O运算符问题,谢谢大虾们~~

来源:百度文库 编辑:中科新闻网 时间:2024/05/10 23:07:19
#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,为什么也??
友元函数可以访问私有数据斗嘛~~~~