chujiangke:重载了上=号操作符号 好像有有关问题 大家帮调速上
chujiangke:重载了下=号操作符号 好像有问题 大家帮调速下
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class student{
int no;
char *name;
public:
student();
student(int ino,string sname);
friend ostream& operator<<(ostream &os,student &stu);
friend student& operator=(student&stu1,student&stu2);
int get_no(){return no;}
char *get_name(){return name;}
void put_no(int a){no=a;}
void put_name(char *newname);
~student(){delete name; }
};
student::student(){
no=-1;
name=new char[30];
strcpy(name,"nodata input haha!!!");}
student::student(int ino,string sname):no(ino){
int size=sname.size();
name=new char[size+1];
strcpy(name,sname.c_str());
}
student&operator=(student&stu1,student&stu2)
{
int size=strlen(stu2.name);
stu1.name=char new[size];
for(int i=0;i<size;i++)
{
strcpy(stu1.name,stu2.name);
}
stu1.no=stu2.no;
return stu1;
}
ostream& operator<<(ostream &os,student &stu)
{
os<<stu.no<<endl;
os<<stu.name<<endl;
return os;
}
int main()
{
cout<<"please input the data:\n";
const int size=4;
student a[size];
for(int i=0;i<size;i++)
cout<<a[i]<<endl;
cout<<"now we put the date now"<<endl;
for(int i=0;i<size;i++)
{
string temp;
cout<<"the no is"<<i<<endl;
cout<<"please input the "<<i<<"name:";
cin>>temp;
student stemp(i,temp);
a[i]=stemp;
}
for(int i=0;i<size;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
------解决方案--------------------
=赋值操作符,()函数调用,[]下标操作符,->指针访问类成员,这几个操作符必须通过成员函数重载。你那个是友元函数。
------解决方案--------------------
另外,你的构造函数里申请内存用的是new [];那么析构函数里也应该是delete []吧?
赋值操作符的重载,有四个步骤:
1.检查是否自己赋值给自己:if(this == &stu) return *this;
2.释放原有的内存资源:delete [] name;
3.分配新的内存资源并赋值
4.返回*this
------解决方案--------------------
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class student{
int no;
char *name;
public:
student();
student(int ino,string sname);
friend ostream& operator<<(ostream &os,student &stu);
friend student& operator=(student&stu1,student&stu2);
int get_no(){return no;}
char *get_name(){return name;}
void put_no(int a){no=a;}
void put_name(char *newname);
~student(){delete name; }
};
student::student(){
no=-1;
name=new char[30];
strcpy(name,"nodata input haha!!!");}
student::student(int ino,string sname):no(ino){
int size=sname.size();
name=new char[size+1];
strcpy(name,sname.c_str());
}
student&operator=(student&stu1,student&stu2)
{
int size=strlen(stu2.name);
stu1.name=char new[size];
for(int i=0;i<size;i++)
{
strcpy(stu1.name,stu2.name);
}
stu1.no=stu2.no;
return stu1;
}
ostream& operator<<(ostream &os,student &stu)
{
os<<stu.no<<endl;
os<<stu.name<<endl;
return os;
}
int main()
{
cout<<"please input the data:\n";
const int size=4;
student a[size];
for(int i=0;i<size;i++)
cout<<a[i]<<endl;
cout<<"now we put the date now"<<endl;
for(int i=0;i<size;i++)
{
string temp;
cout<<"the no is"<<i<<endl;
cout<<"please input the "<<i<<"name:";
cin>>temp;
student stemp(i,temp);
a[i]=stemp;
}
for(int i=0;i<size;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
------解决方案--------------------
=赋值操作符,()函数调用,[]下标操作符,->指针访问类成员,这几个操作符必须通过成员函数重载。你那个是友元函数。
------解决方案--------------------
另外,你的构造函数里申请内存用的是new [];那么析构函数里也应该是delete []吧?
赋值操作符的重载,有四个步骤:
1.检查是否自己赋值给自己:if(this == &stu) return *this;
2.释放原有的内存资源:delete [] name;
3.分配新的内存资源并赋值
4.返回*this
------解决方案--------------------