0

我已经把它剪掉了,所以问题仍然存在。我将能够摆脱为我的作业创建的内容,但是,内存管理导致了一些问题。如果我在复制它时运行此代码,则当它转到重载的 << 函数时它会挂起。

我相信我已经正确完成了内存分配,根据在 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;
}
4

1 回答 1

3

呃...这看起来是个坏主意:

Poly Poly::operator*(const Poly &rhs)
{
    Poly temp;
    //...

    temp.set(newcoeff, neworder);

这看起来很糟糕,因为Poly temp设置了一个 0 阶多项式,但Poly::set不会重新分配coeffneworder.

Poly::set应该delete [] coeff并将其重新分配到新订单指定的新大小。

我没有查看其余代码以查看是否还有其他类似的错误,但是这个错误立即跳出来了。

对于更健壮的解决方案,您应该考虑将 替换为int *vector<int>并让编译器和运行时为您管理内存。您甚至不需要跟踪大小,因为vector<>::size()它会自动为您完成。

于 2013-11-11T01:42:51.770 回答