您好,我正在尝试删除班级中的动态内存,但出现以下错误:
a.out(2830) malloc: *** error for object 0x7fb38a4039c0: pointer being freed was not allocated
*在 malloc_error_break 中设置断点以调试 Abort 陷阱:6
我认为这与我的重载“=”运算符不直接访问我的类“&”有关。
class Polynomial{
public:
//Constructors / Destructors
Polynomial();
Polynomial(int tempNum, int * tempPoly);
~Polynomial();
//Member Functions & Operator overloading
//Addition
Polynomial operator+(Polynomial& Poly);
//Subtraction
Polynomial operator-(Polynomial& Poly);
//Multiplication
Polynomial operator*(Polynomial& Poly);
//Assignment
Polynomial operator=(Polynomial Poly);
//Insertion
friend ostream& operator<<(ostream& os, Polynomial& Poly);
//Extraction
friend istream& operator>>(istream& is, Polynomial& Poly);
private:
//Data Members
//Int array (degree of polynomial + 1)
int * poly;
int polyNum;
};
Implementation:
Polynomial::Polynomial(){
polyNum = 0;
}
Polynomial::~Polynomial(){
//Delete Dynamic Memory
delete [] poly;
}
Polynomial::Polynomial(int tempNum, int *tempPoly){
poly = new int[tempNum+1];
polyNum = tempNum;
for (int i=0; i < tempNum; i++) {
poly[i] = tempPoly[i];
}
}
//Addition
Polynomial Polynomial::operator+(Polynomial &Poly){
Polynomial temp;
if(polyNum > Poly.polyNum){
temp.polyNum = polyNum;
}
else{
temp.polyNum = Poly.polyNum;
}
temp.poly = new int[temp.polyNum + 1];
for(int i=0; i < temp.polyNum; i++){
temp.poly[i] = Poly.poly[i] + poly[i];
}
return (temp);
}
//Subtraction
Polynomial Polynomial::operator-(Polynomial& Poly){
Polynomial temp;
if(polyNum > Poly.polyNum){
temp.polyNum = polyNum;
}
else{
temp.polyNum = Poly.polyNum;
}
temp.poly = new int[temp.polyNum + 1];
for(int i=0; i < temp.polyNum; i++){
temp.poly[i] = poly[i] - Poly.poly[i];
}
return (temp);
}
//Multiplication
Polynomial Polynomial::operator*(Polynomial& Poly){
Polynomial temp;
//make coefficient array
temp.polyNum = (polyNum + Poly.polyNum) - 1;
temp.poly = new int [temp.polyNum];
for (int i = 0; i < temp.polyNum; i++)
{
for (int j = 0; j < Poly.polyNum; j++){
temp.poly[i+j] += poly[i] * Poly.poly[j];
}
}
return temp;
}
//Assignment
Polynomial Polynomial::operator=(Polynomial Poly){
polyNum = Poly.polyNum;
poly = new int[polyNum+1];
for (int i=0; i < polyNum; i++) {
poly[i] = Poly.poly[i];
}
return *this;
}
//Insertion
ostream& operator<<(ostream& os, Polynomial& Poly){
for (int i=0; i < Poly.polyNum; i++) {
os << Poly.poly[i] << " x^" << i;
if(i != Poly.polyNum - 1){
os << " + ";
}
}
return os;
}
//Extraction
istream& operator>>(istream& is, Polynomial& Poly){
int numP = 0;
int * tempP;
is >> numP;
tempP = new int [numP+1];
for (int i=0; i < numP; i++) {
is >> tempP[i];
}
Poly.polyNum = numP;
Poly.poly = new int[Poly.polyNum +1];
for (int i=0; i < Poly.polyNum; i++) {
Poly.poly[i] = tempP[i];
}
delete [] tempP;
return is;
}
主要的
int main(){
// Input Polynomial #1 (P1)
cout << "Input polynominal p1: " << endl;
Polynomial P1;
cin >> P1;
// Output Polynominal
cout << "p1(x) = " << P1 << '\n' << endl;
// Input Polynomial #2 (P2)
cout << "Input polynominal p2: " << endl;
Polynomial P2;
cin >> P2;
// Output Polynominal
cout << "p2(x) = " << P2 << '\n' << endl;
// Copy P2 to P3 and output P3
Polynomial P3;
P3 = P2;
cout << "Copy p2 to p3, p3(x) = " << P3 << '\n' << endl;
// Add P1 to P2 and output to P3
Polynomial P4;
P4 = P1 + P2;
cout << "p3(x) = p1(x) + p2(x) = " << P4 << '\n' << endl;
// Subtract P1 from P2 and output to P3
Polynomial P5;
P5 = P1 - P2;
cout << "p3(x) = p1(x) - p2(x) = " << P5 << '\n' << endl;
// Subtract P2 from P1 and output to P3
Polynomial P6;
P6 = P2 - P1;
cout << "p3(x) = p2(x) - p1(x) = " << P6 << '\n' << endl;
// Multiply P1 by P2 and output to P3
Polynomial P7;
P7 = P1 * P2;
cout << "p3(x) = p1(x) * p2(x) = " << P7 << '\n' << endl;
return 0;
}