これを切り詰めたので、問題はまだ発生しています。割り当て用に作成したもので問題を解決できますが、メモリ管理が問題を引き起こしています。このコードをコピーしたまま実行すると、オーバーロードされた << 関数に移動するときにハングアップします。
What is the right way to allocate memory in the C++ constructor?によると、メモリ割り当てが正しく完了したと思います。. エラーがどこから来ているのかわかりません。
#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;
}