dreamyt
2019-11-29 16:29
采纳率: 86.7%
浏览 551

函数运行正常,却没法return0是为什么

最近学到类,用类写一个计算工资的题目,题目写完了,但总是在运行结束后要等一段时间,然后返回值不是0而是一串很长的数,求大佬解惑

图片说明

#include <iostream>
#include<cstdlib>
#include"payroll.h"
using namespace std;
int main()
{
int rate,hour,counts=0;
payroll*p=new payroll[7];
payroll*s=p;
for(counts=0;counts<7;counts++,p++)
    {
        cout<<"enter the hourly pay rate of employee "<<counts+1<<":";
        cin>>rate;
        cout<<"enter the working hour of employee "<<counts+1<<":";
        cin>>hour;
        if (hour < 0)
            {
                cout <<"NO NEGATIVE HOURS ARE ALLOWED\n ";
                counts--;
            }
            if (hour> 60)
            {
                cout <<"Valid hours are from 0 - 60\n ";
                counts--;
            }
           (*p).Sethour(hour);
           p->Setrate(rate);

    }
    for(counts=0;counts<7;counts++,s++)
        {cout<<"The gross of employee "<<counts+1<<"is: "<<s->gross()<<endl;}


        delete []p;
        p=NULL;

    return 0;
}


我的destructor里有设置cout<<"success!";可是并没有显示,是不是说明没有成功地删除

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • ysuwood 2019-11-29 18:07
    已采纳

    delete s;删掉

    p是new申请的空间,delete[]p;释放掉了,再delete s就出错了。

    p++; p已经发生变化了,必须恢复最初值才能delete

        p=s;//增加
        for(counts=0;counts<7;counts++,s++)
        {cout<<"The gross of employee "<<counts+1<<"is: "<<s->gross()<<endl;}
    
    
        delete []p;
        p=NULL;
    
        return 0;
    
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • threenewbee 2019-11-29 17:22
    for(counts=0;counts<7;counts++,s++) {
        cout<<"The gross of employee "<<counts+1<<"is: "<<s->gross()<<endl;
        delete p;
        delete s;
                }
                这里缺少花括号
    
                导致程序释放不对,异常退出
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题