简体   繁体   中英

(c++) Need help overloading operator+ for my class Polynomial

So I am trying to overload the operator + for my Polynomial array, and I keep getting _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) error (line 52) and I can't figure out why. The problem must be with the way I overloaded operator+ since it doesn't happen without it.

class Polynomial

    {
    public:
        int compo;
        int expo;
        int size; // array size + largest exponent
        friend ostream &operator<<(ostream&, const Polynomial &);
        friend istream &operator>>(istream&, Polynomial &);
        int* arr;
        Polynomial();
        Polynomial(int);
        Polynomial(const Polynomial&);
        ~Polynomial();
        int getSize();
        Polynomial operator+(const Polynomial &);


    private:


    };

...

Polynomial Polynomial::operator+(const Polynomial &p)
{
    Polynomial p1;
    for (int i = 0; i < p1.size; i++)
    {
        p1.arr[i] = arr[i] + p.arr[i];
    }
    return p1;
}

main (just for checking what's going on):

int main()
{
    Polynomial p;
    p.arr[2] = 4;
    p.arr[0] = 4;
    cout << p;
    cout << "Enter compo, expo:\n";
    cin >> p;
    Polynomial z;
    z = z + p;
    cout << z;
    return 0;
}

constructors:

Polynomial::Polynomial()
{
    size = 100;
    arr = new int[size];
}

Polynomial::Polynomial(int a)
{
    size = a;
    arr = new int[size];
}

Copy constructor:

Polynomial::Polynomial(const Polynomial& p)
{
    arr = p.arr;
    size = p.size;
}

Destructor:

Polynomial::~Polynomial()
{
    delete[] arr;
}

Cin:

istream &operator>>(istream &input, Polynomial &p)
{
    input >> p.compo;
    input >> p.expo;
    if (p.expo > p.size+1)
    {
        int *temp;
        p.size = p.expo + 1;
        temp = new int[p.expo + 1];
        for (int i = 0; i < p.expo+1; i++)
        {
            temp[i] = p.arr[i];
        }
        delete[] p.arr;
        p.arr = temp;
    }
    p.arr[p.expo] = p.compo;
    return input;
}

There seems to be problem with copy constructor.

Polynomial::Polynomial(const Polynomial& p)
{
    arr = p.arr;
    size = p.size;
}

With this, you assign pointers, not arrays. Do it in a loop, element by element

Polynomial::Polynomial(const Polynomial& p)
{
    compo = p.compo;
    expo = p.expo;
    size = p.size;
    arr = new int[size];
    for(int i = 0; i< size; ++i)
        arr[i] = p.arr[i];

}

How to write an easy operator + in a polynomial-like class:

Step 1: do not mix storage logic with other logic. Storage is tricky enough. Store your data in a std::vector , and use its size as your size.

Step 2: the rule of zero. Either fail to implement, or =default , your move/copy assign and ctors. Sometimes implement a dtor/zero arg ctor, but allocate no resources there: step 1 covers that. ( =default may not be needed, but sometimes it is. Rather than remebering when it is needed, just do it when you actually want the default xopy/move explicitly).

Step 3: Implement operator+= . It is easier than operator+ almost always. It should be 4 lines long:

Foo& operator+=(Foo const& rhs){
  storage.resize((std::max)(storage.size(),rhs.storage.size());
  for(size_t i=0; i<rhs.storage.size();++i)
    storage[i]+=rhs.storage[i];
  return *this;
}

Code to trim leading zeros can also be added.

Step 4: Implement operator+ as follows:

friend Foo operator+(Foo lhs, Foo const& rhs){
  lhs+=rhs;
  return std::move(lhs);
}

and you are done. (replace Foo with your type name). You even get highly efficient r = a+b+c+d out of it.


Your copy ctor may be broken, but I maintain your problem is actually that you have a copy ctor more than it having a bug in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM