高手们,菜鸟问题多!!

#include <iostream>
#include <string>
using namespace std;
struct record
{
string name;
string tel;
friend istream& operator >>(istream& is, record* t)
{
is>>t->name;
is>>t->tel;
return is;
}
friend ostream& operator<<(ostream& os,const record* t)
{
os<<t->name<<'\t'<<t->tel;
return os;
}
};
int main()
{
record *t;
cout<<"sdfsf"<<endl;
cin>>t;//为什么运行的时候到这句就错了呢?重载有什么问题?
cout<<t<<endl;
cout<<"sdfsf"<<endl;
system("pause");
return 0;

}

record *t;
t是没有指向的指针,没有内存为其保存数据,所以出错!

修改为:
record *t = new record();//最好添加个空的构造函数

然后在
return 0;
前添加:
delete t;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-03-12
#include <iostream>
#include <string>
using namespace std;
struct record
{
string name;
string tel;
friend istream& operator >>(istream& is, record& t)
{
is>>t.name;
is>>t.tel;
return is;
}
friend ostream& operator<<(ostream& os,const record& t)
{
os<<t.name<<'\t'<<t.tel;
return os;
}
};
int main()
{
record t;
cout<<"sdfsf"<<endl;
cin>>t;//为什么运行的时候到这句就错了呢?重载有什么问题?

cout<<t<<endl;
cout<<"sdfsf"<<endl;
system("pause");
return 0;

}

是我我就传引用,什么事都没有。
指针没有被初始化,所以你执行的时候会出错。用指针不会自动分配控件啊,你给它New一下,看下面这个:
#include <iostream>
#include <string>
using namespace std;
struct record
{
string name;
string tel;
friend istream& operator >>(istream& is, record* t)
{
is>>t->name;
is>>t->tel;
return is;
}
friend ostream& operator<<(ostream& os,const record* t)
{
os<<t->name<<'\t'<<t->tel;
return os;
}
};
int main()
{
record *t = new record();
cout<<"sdfsf"<<endl;
cin>>t;//为什么运行的时候到这句就错了呢?重载有什么问题?
cout<<t<<endl;
cout<<"sdfsf"<<endl;
system("pause");

delete t;
return 0;

}