先上代码
#include <iostream.h> #include <string> class mystring{ char * pstr; int length; public: mystring(){ pstr=NULL; length =0;} mystring(char * p,int n){ pstr = new char[n]; pstr= strcpy(p,pstr); } ~mystring(){delete[]pstr;} mystring & operator = (const mystring & temp){ if(pstr!=NULL) delete [] pstr; length=temp.length; pstr=new char[length+1]; strcpy(pstr,temp.pstr); return *this; } friend ostream & operator<<(ostream & os,mystring & temp); friend istream & operator>>(istream & is,mystring & temp); };
ostream & operator<<(ostream & os,mystring & temp){
cout<<temp.pstr;
return os;
}
istream & operator>>(istream & is,mystring & temp){
char p[10000];
if((temp.pstr)!=NULL)
delete []temp.pstr;
cout<<"请输入字符串内容:"<
cin>>p;
temp.pstr=new char[strlen(p)+1];
strcpy(temp.pstr,p);
temp.length=strlen(p);
return is;
}
void main(){
mystring a,b;
cin>>a;
b=a;
cout<<"b="<<b<<endl;
}
![图片说明](https://img-ask.csdn.net/upload/202004/22/1587557151_182844.png)
有取址符不报错
![图片说明](https://img-ask.csdn.net/upload/202004/22/1587557177_469077.png)
没有取址符,报错了
我想问各位大佬,为什么没有取址符这里报错了