_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)错误

时间:2016-09-28 02:08:29

标签: c++ visual-studio

我知道这里有很多关于这个错误的问题,但我一般都是c ++和指针/内存管理的新手,而且真的无法解决这个问题。 这是关于运算符覆盖的c ++类的赋值,并且我在复制构造函数和赋值运算符方面遇到了很多麻烦,并且在调用复制构造函数时似乎抛出此异常。这是我下面的所有代码,抱歉缺少评论

#include "polynomial.h"

Polynomial::Polynomial()
{
        double d[] = {0};
    degree = 0;
    coef = d;
}

Polynomial::Polynomial(double d[], int degree)
{
    this->degree = degree;
    coef = d;
}

Polynomial::Polynomial(const Polynomial & rhs)
{
    double *n;

    this->degree = rhs.degree;

    n = new double[this->degree +1];
    for(int i =0; i<this->degree; i++)
    {
        n[i] = rhs[i];
    }
    this->coef = n;
}

Polynomial::~Polynomial()
{
    delete coef;
    coef = NULL;
}

int Polynomial::getDegree() const
{
    return this->degree;
}

double & Polynomial::operator [](int term)
{
    if(term > degree)
    {
        cout<<"degree out of bounds error"<<endl;
        exit(1);
    }
    return *(coef + term);
}

const double & Polynomial::operator [](int term) const
{
        if(term > degree)
    {
        cout<<"term out of bounds error"<<endl;
        exit(1);
    }
    return *(coef + term);
}

double Polynomial::evaluate(double x)
{
    Polynomial k(*this);
    double sum = 0;
    for(int i = 0; i<=degree; i++)
    {
        sum += k[i] * pow(x,i);
    }

    return sum;
}

Polynomial operator +(Polynomial & p1, Polynomial & p2)
{
    double *n;
    if(p1.degree == p2.degree)
    {
        n = new double[p1.degree + 1];
        for(int i = 0; i<=p1.degree;i++)
            {
        n[i] = p1[i] + p2[i];
                }
        Polynomial k(n,p1.degree);
        return k;
    }
    else if( p1.degree > p2.degree)
    {
         n = new double[p1.degree + 1];
         int i =0;
         while(i<=p2.degree)
         {
             n[i] = p1[i] + p2[i];
             i++;
         }
         while(i<=p1.degree)
         {
            n[i] = p1[i];
         }
         Polynomial k(n,p1.degree);
    }
        else if( p2.degree > p1.degree)
    {
         n = new double[p2.degree + 1];
         int i =0;
         while(i<=p1.degree)
         {
             n[i] = p1[i] + p2[i];
             i++;
         }
         while(i<p2.degree)
         {
             n[i] = p1[i];
         }
         Polynomial k(n,p2.degree);
    }
}

Polynomial operator -(Polynomial & p1, Polynomial & p2)
{
    double * n;
    if(p1.degree == p2.degree)
    {
        n = new double[p1.degree + 1];
        for(int i = 0; i<=p1.degree;i++)
        {
            n[i] = p1[i] - p2[i];
        }
        Polynomial k(n,p1.degree);
        return k;
    }
    else if( p1.degree > p2.degree)
    {
         n = new double[p1.degree + 1];
         int i =0;
         while(i<=p2.degree)
         {
             n[i] = p1[i] - p2[i];
             i++;
         }
         while(i<=p1.degree)
         {
             n[i] = p1[i];
         }
         Polynomial k(n,p1.degree);
    }
        else if( p2.degree > p1.degree)
    {
         n = new double[p2.degree + 1];
         int i =0;
         while(i<=p1.degree)
         {
             n[i] = p1[i] - p2[i];
             i++;
         }
         while(i<=p2.degree)
         {
             n[i] = 0 - p1[i];
         }
         Polynomial k(n,p2.degree);
    }
}

Polynomial operator *(Polynomial & p1, Polynomial & p2)
{
    double *n;
    n = new double[p1.degree + p2.degree +1];
    for(int i = 0; i<=p1.degree; i++)
    {
        for(int j = 0; j<=p2.degree; j++)
        {
            n[i+j] += p1[i] + p2[j];
        }
    }
    Polynomial k(n, (p1.degree + p2.degree));
    return k;
}

ostream & operator <<(ostream & out, Polynomial & p)
{
    string s();
    for(int i = 0; i<=p.degree;i++)
    {
        out<< p[i] << "X^" << i;
        if(i<p.degree)
        {
            out<<" + ";
        }
    }
    return out;
}

Polynomial & Polynomial::operator =(Polynomial p) 
{

    swap(*this,p);
    return *this;
}


void swap(Polynomial & p1, Polynomial & p2)
{
    using std::swap;

    swap(p1.degree,p2.degree);
    swap(p1.coef,p2.coef);
}

0 个答案:

没有答案