Heap corruption detected

#ifndef _POLY_H

#define _POLY_H

#include<iostream>
using std::ostream;
using std::istream;


#include<vector>

using namespace std;

class Polynomial

{

private:

int deg;

double* coeff;



public:

Polynomial();

Polynomial(int degree, const vector<double> coefficients);

Polynomial(const Polynomial&);

~Polynomial();



const Polynomial& operator=(const Polynomial &p);

bool operator==(const Polynomial&) const;



int getDegree() const;

friend ostream& operator<<(ostream&, const Polynomial&);

friend istream& operator>>(istream&, Polynomial&);

};

#endif


Polynomial.cpp
#include "Polynomial.h"

#include<iostream>
using std::cout;
using std::endl;
using std::cin;


Polynomial::Polynomial()

{
deg = -1;

coeff = NULL;

}

Polynomial::Polynomial(int degree, const vector<double> coefficients)

{
deg = degree;

coeff = new double[degree];

for (int i = degree; i >= 0; i--)

{

coeff[i] = coefficients[i];

}

}

Polynomial::Polynomial(const Polynomial &p)

{
deg = p.deg;

coeff = new double[deg];

for (int i = deg; i >= 0; i--)

{

coeff[i] = p.coeff[i];

}

}

Polynomial::~Polynomial()

{
delete[] coeff;

}

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

{
Polynomial Po(p);

return Po;

}

bool Polynomial::operator==(const Polynomial& p) const

{
if (deg != p.deg)

{
return false;

}

else

{

for (int i = p.deg; i >= 0; i--)

{

if (coeff[i] != p.coeff[i])

{

return false;

}

}

}

return true;

}

int Polynomial::getDegree() const

{

return deg;

}



ostream& operator<<(ostream& os, const Polynomial& p)

{

if (p.deg == -1)

{

os << "empty" << endl;

}

else

{

for (int i = p.deg; i>0; i--)

{

if (p.coeff[i]>0)

{

os << "+" << p.coeff[i] << "x^(" << i << ") ";

}

else if (p.coeff[i]<0)

{

os << p.coeff[i] << "x^(" << i << ") ";

}

}

if (p.coeff[0]>0)

{

os << "+" << p.coeff[0] << endl;

}

else if (p.coeff[0]<0)

{

os << p.coeff[0] << endl;

}

}

return os;

}

istream& operator>>(istream& is, Polynomial& p)

{

cout << "Enter the polynomial (integer degree then double coefficients):" << endl;

is >> p.deg;

p.coeff = new double[p.deg];

for (int i = p.deg; i >= 0; i--)

{

is >> p.coeff[i];

}

return is;

}



Prog10.cpp
#include<iostream>

#include<vector>

using std::cout;
using std::endl;
using std::cin;

#include "Polynomial.h"

int main()

{

Polynomial A;

cout << "(1) Testing `cout << A': " << A << endl;

cout << "(2) Testing `cin >> A':\n";

cout << "Enter the polynomial (integer order then double coefficients):\n\t ";

cin >> A;

cout << endl;

cout << "(3) Second look at A: " << A << endl;



Polynomial B(A);

cout << "(4) Testing Polynomial B(A)': " << B << endl;



vector<double> clist;

clist.push_back(1);

clist.push_back(4.5);

clist.push_back(8);

Polynomial C(2, clist);

cout << "(5) Testing `Polynomial C(2, clist)': " << C << endl;



Polynomial D = C;

cout << "(6) Testing D = C: " << D << endl;



cout << "(7) Testing A == B : " << (A == B ? "TRUE" : "FALSE") << endl;

cout << "(7) Testing A == D : " << (A == D ? "TRUE" : "FALSE") << endl;

return 0;

}
Above I attached my Polynomial.h file, Polynomial.cpp file andProg10.cpp file. However after compiling I got an error called Microsoft Visual c++ Runtime library in which it says Debug error Program name unknown Heap CORRUPTION DETECTED after normal block. pLEASE help me in this
type array[n];
valid index goes from 0 to n-1 (count them, you've got `n' valid index values)
yet all over you code you try to access position `n' that's out of bounds


also, ¿why the assignment operator doesn't modify the object?
1
2
3
4
5
const Polynomial& Polynomial::operator=(const Polynomial& p)
{
   Polynomial Po(p);
   return Po;
}
Last edited on
You already include vector.

> double* coeff;
So make this
vector<double> coeff;

and the whole problem goes away.

also, ¿why the assignment operator doesn't modify the object?
1
2
3
4
5
const Polynomial& Polynomial::operator=(const Polynomial& p)
{
   Polynomial Po(p);
   return Po;
}


What object should be modified in this code? This code creates a WHOLE NEW Polynomial object, named Po. Nothing is being modified.

Why is it creating a whole new object? The assignment operator should not create new objects; it should modify an object that already exists.
> What object should be modified in this code?
*this
Topic archived. No new replies allowed.