我已经把它剪掉了,所以问题仍然存在。我将能够摆脱为我的作业创建的内容,但是,内存管理导致了一些问题。如果我在复制它时运行此代码,则当它转到重载的 << 函数时它会挂起。
我相信我已经正确完成了内存分配,根据在 C++ 构造函数中分配内存的正确方法是什么?. 我不知道错误来自哪里。
#include <iostream>
#include <iomanip>
using namespace std;
class Poly
{
private:
int order; //order of the polynomial
int size; //order + 1
int * coeff;//pointer to array of coeff on the heap
public:
Poly();
Poly(int Order);
Poly(int Order, int * Coeff);
~Poly(){delete [] coeff; cout << "Destructor\n";};
Poly(const Poly &rhs);
//accessors & mutators
void set(int * Coeff, int Order);
//Overloaded Operators
Poly operator+(const Poly &rhs);
Poly & operator=(const Poly &rhs);
friend ostream & operator<<(ostream & Out, const Poly &rhs);
};
int main()
{
int coeff1[ ] = {-38,2,-24,6,4};
int coeff2[ ] = {-38,2,-14,0,0,10,0,4};
int size;
bool flag;
Poly P1(4, coeff1);
Poly P2(7, coeff2);
Poly P3;
P3 = P1 + P2;
cout << "P1 + P2: " << P3;
return 0;
}
Poly::Poly()
{
order = 0;
size = order + 1;
coeff = new int[size];
coeff[0] = 0;
cout << "Default Constructor\n";
}
Poly::Poly(int Order)
{
order = Order;
size = order + 1;
coeff = new int[size];
for(int i(0); i <= order; i++)
coeff[i] = 0;
cout << "Order Constructor\n";
}
Poly::Poly(int Order, int * Coeff)
{
order = Order;
size = order + 1;
coeff = new int[size];
for(int i(0); i <= order; i++)
coeff[i] = Coeff[i];
cout << "Complete Constructor\n";
}
Poly::Poly(const Poly &rhs)
{
order = rhs.order;
size = rhs.size;
int *coeff = new int[size];
for(int i(0); i <= order; i++)
coeff[i] = rhs.coeff[i];
cout << "Copy Constructor\n";
}
void Poly::set(int * Coeff, int Order)
{
order = Order;
size = order + 1;
for(int i(0); i <= order; i++)
coeff[i] = Coeff[i];
}
Poly Poly::operator+(const Poly &rhs)
{
int neworder = max(order, rhs.order);
int * newcoeff = new int[neworder+1];
Poly temp(neworder, newcoeff);
delete [] newcoeff;
for(int i(0); i <= temp.order; i++)
temp.coeff[i] = 0;
for(int i(0); i <= order; i++)
temp.coeff[i] = coeff[i];
for(int i(0); i <= rhs.order; i++)
temp.coeff[i] += rhs.coeff[i];
return Poly(temp.order, temp.coeff);
}
ostream &operator <<(ostream& out, const Poly &source)
{
for(int i(source.order); i >= 0; i--)
{
if(i == 1)
{
if(i == source.order)
if(source.coeff[i] == 1)
out << "X";
else if(source.coeff[i] == -1)
out << "-X";
else
out << source.coeff[i] << "X";
else if(source.coeff[i] == 1)
out << " + " << "X";
else if(source.coeff[i] == -1)
out << " - " << "X";
else if(source.coeff[i] > 0)
out << " + " << source.coeff[i] << "X";
else if(source.coeff[i] < 0)
out << " - " << abs(source.coeff[i]) << "X";
}
else if(i > 1)
{
if(i == source.order)
if(source.coeff[i] == 1)
out << "X^" << i;
else if(source.coeff[i] == -1)
out << "-X^" << i;
else
out << source.coeff[i] << "X^" << i;
else if(source.coeff[i] == 1)
out << " + " << "X^" << i;
else if(source.coeff[i] == -1)
out << " - " << "X^" << i;
else if(source.coeff[i] > 1)
out << " + " << source.coeff[i] << "X^" << i;
else if(source.coeff[i] < -1)
out << " - " << abs(source.coeff[i]) << "X^" << i;
}
else
{
if(source.coeff[i] > 0)
out << " + " << source.coeff[i];
else if(source.coeff[i] < 0)
out << " - " << abs(source.coeff[i]);
}
}
out << endl;
return out;
}
Poly & Poly::operator=(const Poly &rhs)
{
order = rhs.order;
for(int i(0); i <= rhs.order; i++)
coeff[i] = rhs.coeff[i];
return *this;
}