这个析构函数哪里错了?

austinlui2 2009-12-10 06:30:13
调试了下,错误定位在析构函数,大家帮忙看下..........为了方便,代码全附上,应付作业..用动态数组求多项式...写得很烂,别见笑
头文件:
#ifndef POLY_H
#define POLY_H
#include<iostream>
using namespace std;
struct term
{
double coef;
int exp;
};
class poly
{
public:
poly(const int n);
poly(const poly& p);
poly();
~poly();
friend poly operator +(const poly p1, const poly p2);
friend poly operator -(const poly p1, const poly p2);
friend poly operator *(const poly p1, const poly p2);
void operator = (const poly p);
double result(double x);
void getCoef();
void setCoef();
private:
int max;
term* expression;
};

#endif


实现文件
#include<iostream>
#include<cmath>
#include"poly.h"
using namespace std;
poly::poly(const int n)
{
max = n;
expression = new term [max];

}

poly::poly()
{
max = 1;
expression = new term [max];
expression[0].coef=expression[0].exp =0;

}

void poly::operator =(const poly p)
{
int i = 0;
max = p.max;
while(i<max)
{
expression[i].coef = p.expression[i].coef;
expression[i].exp = p.expression[i].exp;
i++;
}
}

poly::poly(const poly &p)
{
max = p.max;
expression = new term [max];
int i = 0;
while(i < max)
{
expression[i].coef = p.expression[i].coef;
expression[i].exp = p.expression[i].exp;
i++;
}

}

poly::~poly()
{
delete [] expression;
}


poly operator +(const poly p1, const poly p2)
{
int n;
if(p1.max>p2.max)
n = p1.max;
else
n = p2.max;
poly temp(n);
int i = 0,j = 0,t =0;
while(t <temp.max)
{
if(p1.expression[i].exp < p2.expression[j].exp)
{
temp.expression[t].coef = p2.expression[j].coef;
temp.expression[t].coef = p2.expression[j].coef;
t++;
j++;

}
else if(p1.expression[i].exp> p2.expression[j].exp)
{
temp.expression[t].coef = p1.expression[i].coef;
temp.expression[t].exp = p1.expression[i].exp;
t++;
i++;
}
else
{
temp.expression[t].coef = p1.expression[i].coef+p2.expression[j].coef;
temp.expression[t].exp = p1.expression[i].exp+p2.expression[j].exp;
t++;
i++;
j++;
}
}
while(i <p1.max)
{
temp.expression[t].coef = p1.expression[i].coef;
temp.expression[t].exp = p1.expression[i].exp;
t++;
i++;
}
while(j<p2.max)
{
temp.expression[t].coef = p2.expression[j].coef;
temp.expression[t].exp = p2.expression[j].exp;
t++;
j++;
}
return temp;
}



poly operator -(const poly p1, const poly p2)
{
int n;
if(p1.max>p2.max)
n = p1.max;
else
n = p2.max;
poly temp(n);
int i = 0,j = 0,t =0;
while(t <temp.max)
{
if(p1.expression[i].exp < p2.expression[j].exp)
{
temp.expression[t].coef = -p2.expression[j].coef;
temp.expression[t].exp = -p2.expression[j].exp;
t++;
j++;

}
else if(p1.expression[i].exp> p2.expression[j].exp)
{
temp.expression[t].coef = p1.expression[i].coef;
temp.expression[t].exp = p1.expression[i].exp;
t++;
i++;
}
else
{
temp.expression[t].coef = p1.expression[i].coef-p2.expression[j].coef;
temp.expression[t].exp = p1.expression[i].exp-p2.expression[j].exp;
t++;
i++;
j++;
}
}
while(i <p1.max)
{
temp.expression[t].coef = p1.expression[i].coef;
temp.expression[t].exp = p1.expression[i].exp;
t++;
i++;
}
while(j<p2.max)
{
temp.expression[t].coef = -p2.expression[j].coef;
temp.expression[t].exp = -p2.expression[j].exp;
t++;
j++;
}
return temp;
}
/*poly operator *(const poly p1, const poly p2);*/

double poly::result(double x)
{
double sum = 0;
int i = 0;
while(i<max)
{
sum += pow(x, expression[i++].exp);
}
return sum;
}
void poly::getCoef()
{
int i = 0;
while(i<max)
{
cout<<expression[i++].coef<< " ";
}
cout<<endl;
}
void poly::setCoef()
{
int i = 0;
while(i<max)
{
cin>>expression[i++].coef;
}
}


主文件
#include<iostream>
#include"poly.h"

using namespace std;

int main()
{
poly p1(5);
p1.setCoef();
poly p2(p1);
p1.getCoef();
p2.getCoef();
poly p3;
p3 = p1+p2;
p3.getCoef();
return 0;

}
...全文
108 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
huahenyueying 2009-12-11
  • 打赏
  • 举报
回复
这个问题是这样造成的:
poly p3;//你的无参数构造函数分配了1个空间
delete [] expression 就会出错
正确的应该是 delete expression,如果想办法在析构函数函数里加上这句delete expression就不会出错.

不过建议你这么改:
void poly::operator =(const poly p)
{
if (expression != NULL)
{
if (max >1 )
{
delete [] expression;
}
else
{
delete expression;
}
expression = NULL;
}
max = p.max;
expression = new term [max];
int i = 0;
while(i<max)
{
expression[i].coef = p.expression[i].coef;
expression[i].exp = p.expression[i].exp;
i++;
}
}


poly::~poly()
{
if (expression !=NULL )
{
delete [] expression;
}
expression =NULL;
}


删除一个指针后,一定要将指针置为空,不然会成野指针

wyzhao0102 2009-12-10
  • 打赏
  • 举报
回复
可能是野指针问题
看看有没有把一个对象赋给另外一个
我猜测的
austinlui2 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 deng1243 的回复:]
C/C++ code
poly::poly()
{
max=1;//这里才一个空间 expression=new term [max];
expression[0].coef=expression[0].exp=0;

}
C/C++ codeint main()
{
poly p1(5);
p1.setCoef();
poly p2(p1);
p1.getCoef();
p2.getCoef();
poly p3;//调用构造函数poly(),或改为poly p3(5),或修改无参构造函数poly() p3= p1+p2;//相加后空间越界了 p3.getCoef();return0;

}
[/Quote]

我重载了 + = 这些东西 , 里面会动态分配空间的。。。难道不对么?
austinlui2 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mmilmf 的回复:]
C/C++ code
poly::~poly()
{//delete [] expression;注释掉}
[/Quote] 这个我知道,但是规定得手动写个析构函数...
deng1243 2009-12-10
  • 打赏
  • 举报
回复

poly::poly()
{
max = 1;//这里才一个空间
expression = new term [max];
expression[0].coef=expression[0].exp =0;

}


int main()
{
poly p1(5);
p1.setCoef();
poly p2(p1);
p1.getCoef();
p2.getCoef();
poly p3;//调用构造函数poly(),或改为poly p3(5),或修改无参构造函数poly()
p3 = p1+p2;//相加后空间越界了
p3.getCoef();
return 0;

}
mmilmf 2009-12-10
  • 打赏
  • 举报
回复

poly::~poly()
{
//delete [] expression;注释掉
}

forster 2009-12-10
  • 打赏
  • 举报
回复
头文件呢 其他都没用

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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