Polynomial Class Crashes While Handling overloaded operator=

0

I am working on a program that derives a Polynomial class from a Function class. I am almost complete, but I'm running in to one last problem.

I am invoking an "=" operator. In function main, it looks like this:

f3 = f2 * 2;

f3 and f2 are both Polynomial data types. The multiply operator is overloaded, as well, and has been tested to work. When my program reaches this point, it crashes. Here is the backtrace of the crash:

Program received signal SIGABRT, Aborted.
0x00000000 in ?? ()
(gdb) bt
#0  0x00000000 in ?? ()
#1  0x77cbf8c1 in ntdll!RtlUpdateClonedSRWLock ()
   from /cygdrive/c/Windows/system32/ntdll.dll
#2  0x75ab0816 in WaitForSingleObjectEx ()
   from /cygdrive/c/Windows/syswow64/KERNELBASE.dll
#3  0x000000ec in ?? ()
#4  0x00000000 in ?? ()

Unfortunately, that does not provide me any hints.

Lastly, here is the overloaded operator=:

    Polynomial& Polynomial::operator=(const Polynomial& rhs) {
if(this->a)
    delete [] this->a;
this->setName(rhs.getName());
this->degree = rhs.degree;
a = new double[this->degree];
for(int i = 0; i < this->degree; i++)
{
    this->a[i] = rhs.a[i];
}
return *this;
}

And the header file for the class.

    class Polynomial : public Function
{
    public:
                    Polynomial (const string& name = "unnamed");
                    Polynomial (const Polynomial& rhs);
        void        setCoefficients (int degree, ...);
        int         getDegree() const { return degree; }
        double      operator[] (int i) const;
        double      operator() (double x) const;
        Polynomial  operator* (double c) const;
        Polynomial& operator*= (double c);
        Polynomial& operator=(const Polynomial& rhs);
        void        write(ostream& outfile) const;

    private:
        double*    a;
        int        degree;
};
Polynomial operator* (double c, const Polynomial& p);

Constructors:

   (Polynomial::Polynomial (const string& name) {
    a = new double[1];
    a[0] = 0;
    degree = 0;
    return;
    }

Polynomial::Polynomial (const Polynomial& rhs) {

    //Null a, and then pass over to the overloaded = operator.
    if(this->a)
        delete [] this->a;
    *this = rhs;
    return;
    }

I'm willing to share as much code as necessary. Thank you for any help!

c++
asked on Stack Overflow Nov 30, 2012 by mr0il • edited Nov 30, 2012 by mr0il

2 Answers

1

You have both problems with constructor and copy constructor. Copy constructor is constructing object from nothing as normal constructor(but then it copies corresponding values from other object) so you shouldn't check a value there, just create it. There is no need in blank return operator. Also you have a problem with uncertancy of 0 and 1 size of a. This code would be more common:

Polynomial::Polynomial (const string& name) : Function(name) {
    a = 0;
    degree = 0;
}

Polynomial::Polynomial (const Polynomial& rhs) {

    setName(rhs.getName());
    degree = rhs.degree;
    if(degree)
    {
        a = new double[degree];
        for(int i = 0; i < degree; i++)
            a[i] = rhs.a[i];
    }
    else
        a = 0;
}

const Polynomial& Polynomial::operator=(const Polynomial& rhs) {
    if(this != &rhs)
    {
        if(a)
            delete [] a;
        setName(rhs.getName());
        degree = rhs.degree;
        a = new double[degree];
        for(int i = 0; i < degree; i++)
            a[i] = rhs.a[i];
    }
    return *this;
}
answered on Stack Overflow Nov 30, 2012 by Arsenii Fomin • edited Nov 30, 2012 by Arsenii Fomin
1

I think the problem is in your copy constructor, in your copy constructor you have:

//Null a, and then pass over to the overloaded = operator.
if(this->a)
    delete [] this->a;

But a is not initialized here, so you can't delete it, so replace it with:

Polynomial::Polynomial(Polynomial const& other) : a( NULL ) {...}
answered on Stack Overflow Nov 30, 2012 by BigBoss

User contributions licensed under CC BY-SA 3.0