含指针对象的数组析构的问题

qq_37751920 2017-05-24 09:16:07
#include <iostream>
#include <string.h>
#include <iomanip>

using namespace std;

class Note
{
private:
int year, month, day, hour, minute, second;
char *event;
public:
Note(char * msg, int year, int month, int day, int hour=0, int minute=0, int second=0);
Note(){};
Note(const Note & note);
~Note();
void ShowNote();
Note& operator=(const Note &str);
};
Note& Note::operator=(const Note &str)
{
event = new char[strlen(str.event)+1];
strcpy(event,str.event);
year=str.year;
month=str.month;
day=str.day;
hour=str.hour;
minute=str.minute;
second=str.second;
return *this;
}
Note::Note(char * msg, int year, int month, int day, int hour, int minute, int second)
{
event = new char[strlen(msg)+1];
strcpy(event,msg);
this->year=year;
this->month=month;
this->day=day;
this->hour=hour;
this->minute=minute;
this->second=second;
}
Note::Note(const Note¬e)
{
event = new char[strlen(note.event)+1];
strcpy(event,note.event);
year=note.year;
month=note.month;
day=note.day;
hour=note.hour;
minute=note.minute;
second=note.second;
}
void Note::ShowNote()
{
cout<<year<<"-"<<setw(2)<<setfill('0')<<month<<"-"<<setw(2)<<setfill('0')<<day<<" "<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second<<" "<<event<<endl;
}
Note::~Note()
{
delete[]event;
cout<<"destroy Note"<<endl;
}



class NoteList
{
private:
Note noteArr[10];
int noteCount;
public:
NoteList();
void AddNote(Note n);
Note& DeleteFirst();
void PrintAll();
int GetNoteCount();
};
NoteList::NoteList()
{
noteCount = 0;
}

void NoteList::AddNote(Note n)
{

noteArr[noteCount++]=n;

}
Note& NoteList::DeleteFirst()
{
noteCount--;
return noteArr[noteCount];
}
void NoteList::PrintAll()
{
for(int i=0;i<noteCount;i++)
{
noteArr[i].ShowNote();
}

}
int NoteList::GetNoteCount()
{
return noteCount;
}

int main()
{
char eventMsg[100];
Note n2;
int year,month,day,hour,minute,second;
int i,n;
NoteList notes;
int op;
cin >> op;
int x=1;
while (x--)
{
switch (op)
{
case 1:
{
cin>>year;
cin>>month;
cin>>day;
cin>>hour;
cin>>minute;
cin>>second;
cin.get();
cin.getline(eventMsg,90);
Note nt(eventMsg,year,month,day,hour,minute,second);
notes.AddNote(nt);
break;
}
case 2:
{
if (notes.GetNoteCount()>0)
{
Note nt=notes.DeleteFirst();
nt.ShowNote();
}
break;

}
case 3:
{
int cnt = notes.GetNoteCount();
cout<<cnt<<" notes:"<<endl;
notes.PrintAll();
break;
}
}
}
return 0;
}
析构时运行失败
显示结构为:
13个 destroy Note
然后报错,希望大佬解释。
...全文
193 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-05-25
  • 打赏
  • 举报
回复
仅供参考:
#include<iostream>
using namespace std;
const int N=100;
class  Set {
    int *elem;   //存放集合元素的指针
    int count;   //存放集合中的元素个数
public:
    Set();                      //默认构造
    Set(int s[],int n);         //构造
    Set::Set(const Set&s);      //拷贝构造
    ~Set();                     //析构
    int find(int x)const;       //判断x是否在集合中
    Set operator+(const Set&);  //集合的并集
    Set operator-(const Set&);  //集合的差集
    Set operator*(const Set&);  //集合的交集
    Set &operator=(const Set&); //重载赋值运算符
    void disp();                //输出集合元素
};
Set::Set() { //默认构造
    elem=NULL;
    count=0;
}
Set::Set(int s[],int n) { //构造
    int i;
    elem=new int[n];
    for(i=0; i<n;i++)
        elem[i]=s[i];
    count=n;
}
Set::Set(const Set&s) { //拷贝构造
    int i;

    if (s.count>0) {
        elem=new int[s.count];
        for(i=0;i<s.count;i++)
            elem[i]=s.elem[i];
    } else elem=NULL;
    count=s.count;
}
Set::~Set() { //析构
    if (count) {delete []elem;count=0;}
}
int Set::find(int x)const { //判断x是否在集合中
    for(int i=0;i<count;i++) {
        if(elem[i]==x)
            return 1;
    }
    return 0;
}
Set Set::operator+(const Set&s) { //集合的并集
    int a[N],k,i;
    for(i=0;i<count;i++) { //先将当前集合元素放入a数组中
        a[i]=elem[i];
    }
    k=count;
    for(i=0;i<s.count;i++) //将集合s中不在当前集合中的元素放入a
        if(find(s.elem[i]))
            continue;
        else {
            if (k==N-1) break;
            a[k++]=s.elem[i];
        }
    return Set(a,k); //调用构造函数产生一个无名对象返回
}
Set Set::operator-(const Set&s) { //集合的差集
    int a[N],k=0,i;
    for(i=0;i<count;i++) //将当前集合中不在s集合中的元素放入a数
        if(!s.find(elem[i]))
            a[k++]=elem[i];
    return Set(a,k); //调用构造函数产生一个无名对象返回
}
Set Set::operator*(const Set &s) { //集合的交集
    int a[N],k=0,i;
    for(i=0;i<count;i++) //扫描当前集合中所有元素
        if(s.find(elem[i]))
            a[k++]=elem[i]; //将属于两个集合的元素放在a数组中
    return Set(a,k); //调用构造函数产生一个无名对象返回
}
Set &Set::operator=(const Set &s) { //重载赋值运算符
    int i;
    if (&s != this ) {
        if (s.count>0) {
            if (count==0) elem=new int[s.count];
            for(i=0;i<s.count;i++)
                elem[i]=s.elem[i];
        }
        count=s.count;
    }
    return *this;
}
void Set::disp() { //输出集合元素
    int i;
    for(i=0;i<count;i++)
        cout<<elem[i]<<" ";
    cout<<endl;
}
int main() {
    int a[]={1,2,3,4,5};
    int b[]={1,3,5,7,9};
    Set s1(a,5),s2(b,5),s3;
    cout<<"集合s1:";
    s1.disp();
    cout<<"集合s2:";
    s2.disp();
    s3=s1+s2;
    cout<<"并集:";
    s3.disp();
    s3=s1*s2;
    cout<<"交集:";
    s3.disp();
    s3=s1-s2;
    cout<<"差集:";
    s3.disp();
    return 0;
}
//集合s1:1 2 3 4 5
//集合s2:1 3 5 7 9
//并集:1 2 3 4 5 7 9
//交集:1 3 5
//差集:2 4
//
qq_37751920 2017-05-24
  • 打赏
  • 举报
回复
引用 1 楼 qq_37751920 的回复:

这里图片。

能再问一下么?我这里在NoteList类里面加上一个
NoteList::~NoteList()
{
delete[]noteArr;
cout<<"destroy noteArr"<<endl;
}
显示如图
那这个新加的析构函数delete是不起作用么?
真相重于对错 2017-05-24
  • 打赏
  • 举报
回复
char eventMsg[100]; Note n2;//这个变量最后会释放,但是它包含的指针,没有初始化,所以出错
qq_37751920 2017-05-24
  • 打赏
  • 举报
回复

这里图片。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧