c++动态创建对象数组问题
nclude<iostream>#include<string>usingnamespacestd;classEmployee{private:stringname;st...
nclude<iostream>
#include<string>
using namespace std;
class Employee{
private:
string name;
string address;
string city;
string postcode;
public:
Employee():name("a"),address("b"),city("c"),postcode("d"){}
Employee(string n,string a,string c,string p):name(n),address(a),city(c),postcode(p){
cout<<"End Entering"<<endl;}
~Employee(){cout<<"End"<<endl;}
void setName(){
string n;
cout<<"Enter Nmae:";
cin>>n;
name=n;}
void display(){
cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;
cout<<"City:"<<city<<endl;
cout<<"Postcode:"<<postcode<<endl;
cout<<"Displaying all"<<endl;}
};
int main(){
Employee * p=new Employee[2];
p[0].display();
cout<<endl;
p[1].display();
delete p;
cout<<endl;
p=new Employee("e","r","t","y");
p[0].display();
cout<<endl;
p[1].display();
delete p;
return 0;
}
图片提示是为什么错的? 展开
#include<string>
using namespace std;
class Employee{
private:
string name;
string address;
string city;
string postcode;
public:
Employee():name("a"),address("b"),city("c"),postcode("d"){}
Employee(string n,string a,string c,string p):name(n),address(a),city(c),postcode(p){
cout<<"End Entering"<<endl;}
~Employee(){cout<<"End"<<endl;}
void setName(){
string n;
cout<<"Enter Nmae:";
cin>>n;
name=n;}
void display(){
cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;
cout<<"City:"<<city<<endl;
cout<<"Postcode:"<<postcode<<endl;
cout<<"Displaying all"<<endl;}
};
int main(){
Employee * p=new Employee[2];
p[0].display();
cout<<endl;
p[1].display();
delete p;
cout<<endl;
p=new Employee("e","r","t","y");
p[0].display();
cout<<endl;
p[1].display();
delete p;
return 0;
}
图片提示是为什么错的? 展开
3个回答
展开全部
#include<iostream>
#include<string>
using namespace std;
class Employee{
private:
string name;
string address;
string city;
string postcode;
public:
Employee():name("a"),address("b"),city("c"锋或),postcode("d"银皮伍){}
Employee(string n,string a,string c,string p):name(n),address(a),city(c),postcode(p){
cout<<"End Entering"<<endl;}
~Employee(){cout<<"End"<<endl;}
void setName(){
string n;
cout<<"Enter Nmae:";
cin>>n;
name=n;}
void display(){
cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;
cout<<"City:"<<city<<endl;
cout<<"Postcode:"<<postcode<<endl;
cout<<"Displaying all"<<endl;}
};
int main(){
Employee * p=new Employee[2];
p[0].display();
cout<<endl;
p[1].display();
delete[] p; //错误,这是一个数组要加[]
cout<<endl;
p=new Employee("e","r","t","y"); //现在的p不是数组了,那下面这样都是错误的
p->display(); //这里p是握运一个指向对象的指针
cout<<endl;
//p[1].display();
delete p;
return 0;
}
#include<string>
using namespace std;
class Employee{
private:
string name;
string address;
string city;
string postcode;
public:
Employee():name("a"),address("b"),city("c"锋或),postcode("d"银皮伍){}
Employee(string n,string a,string c,string p):name(n),address(a),city(c),postcode(p){
cout<<"End Entering"<<endl;}
~Employee(){cout<<"End"<<endl;}
void setName(){
string n;
cout<<"Enter Nmae:";
cin>>n;
name=n;}
void display(){
cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;
cout<<"City:"<<city<<endl;
cout<<"Postcode:"<<postcode<<endl;
cout<<"Displaying all"<<endl;}
};
int main(){
Employee * p=new Employee[2];
p[0].display();
cout<<endl;
p[1].display();
delete[] p; //错误,这是一个数组要加[]
cout<<endl;
p=new Employee("e","r","t","y"); //现在的p不是数组了,那下面这样都是错误的
p->display(); //这里p是握运一个指向对象的指针
cout<<endl;
//p[1].display();
delete p;
return 0;
}
更多追问追答
追问
第一次删除p后,还能让它再指向数组吗?
追答
那你要分配两个类内存就可以了
如
p=new Employee[2];
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自己断点调试看看,先找出是哪句出了问题,再针对错误的地方修改!
更多追问追答
追问
怎么断点调试?自学,不了解……
追答
先建议你把vc++这个工具的调试和看内存部分好好看看!
先设置断点insert breakpoint
再点go
再调出watch和memory仔细一步步运行观察!
一定要会这个哦!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该是内存益扰缺处,看下初始化,或者型李游赋值问题
希望能帮到你,哪里不清楚卜销再问。
如果有帮到你,请选我答案吧!
希望能帮到你,哪里不清楚卜销再问。
如果有帮到你,请选我答案吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询