这是如何了,为什么string存不进去,输出的不正常呢 初学者求帮助!
这是怎么了,为什么string存不进去,输出的不正常呢 菜鸟求帮助!!!!
#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);
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());
}
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;
}
------解决方案--------------------
你没有实现赋值运算符和析构函数
a[i]=stemp;
------解决方案--------------------
#include<cstring>
改成#include<string>
------解决方案--------------------
看漏了,竟然只有析构函数写在了类定义里面。。。这种写法不好啊,最好统一写在一个地方
因为没有重载赋值运算,在 a[i]=stemp;这步会把stemp的name指针直接赋值给a[i],然后一个循环结束stemp析构的时候删除了name的内存,a[i]里面的name也失效了
------解决方案--------------------
赋值运算符必须重载为成员函数
#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);
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());
}
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;
}
------解决方案--------------------
你没有实现赋值运算符和析构函数
a[i]=stemp;
------解决方案--------------------
#include<cstring>
改成#include<string>
------解决方案--------------------
看漏了,竟然只有析构函数写在了类定义里面。。。这种写法不好啊,最好统一写在一个地方
因为没有重载赋值运算,在 a[i]=stemp;这步会把stemp的name指针直接赋值给a[i],然后一个循环结束stemp析构的时候删除了name的内存,a[i]里面的name也失效了
------解决方案--------------------
赋值运算符必须重载为成员函数
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class student{
int no;
char *name;
public:
student();
student(int ino,string sname);
student& operator=(student&stu1);
friend ostream& operator<<(ostream &os,student &stu);
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){