実行時に決定されたサイズでdoubleの配列を初期化することに問題があります。
MyPoly MyPoly::operator *(const MyPoly& other)
{
int newDegree = this->_degree + other._degree;
double array [newDegree] ;
fillArrayWithZeros(array, this->_degree + other._degree);
PolyRep::iterator it1 = this->_poly->begin();
PolyRep::iterator it2 = other._poly->begin();
for (int i = 0; i <= this->_degree; ++i, ++it1)
{
for (int j = 0; j <= other._degree; ++j, ++it2)
{
array[i + j] += (*it1) * (*it2);
}
it2 = other._poly->begin();
}
return MyPoly(array, this->_degree + other._degree);
}
関数の2行目にあります。悪い数字を言うなら-10それはうまくいきます。コンパイルエラーやランタイムエラーはありませんが、プログラムをデバッグすると、配列が空であることがわかります。
次の関数では、配列のサイズは実行時にも決定されますが、初期化は正常に機能します。
MyPoly MyPoly::operator +(const MyPoly& other)
{
int bigDegree = (this->_poly->getDegree() > other._poly->getDegree()) ?
this->_poly->getDegree() : other._poly->getDegree();
double arr [bigDegree];
PolyRep::iterator it1 = this->_poly->begin();
PolyRep::iterator it2 = other._poly->begin();
for (int i = 0; i <= this->_poly->getDegree(); ++i, ++it1)
{
arr[i] = *it1;
}
for (int i = 0; i <= other._poly->getDegree(); ++i, ++it2)
{
arr[i] += *it2;
}
return MyPoly(arr, bigDegree + 1);
}
両方の関数は同じクラスにあります。
誰かが問題は何かを説明できますか