0

I am using operator overloading for the first time and am running into some problems. I have created a class that stores a polynomial as an array of terms, and have attempted to overload operator+ to add two polynomials togeather. Every time the operator+ function tries to return an object, the program breaks.

Problem:

class Polynomial
{
    friend int coefficient( std::string & );
    friend int exponent( std::string );

public:
    explicit Polynomial( size_t = 3 ); // default constructor
    ~Polynomial(); // destructor
    void set( std::string[] ); // sets values of ptr
    std::string get(); // returns string of values from extracted from ptr

    Polynomial operator+( const Polynomial & ); // allows summation of objects

private:
    size_t size; // holds the size of array ptr[]
    std::string *ptr; // points to first element of ptr[]
};

int main()
{
    Polynomial p1(3);
    Polynomial p2 = p1;
}

Also, if I try to pass to a function like Polynomial::someMemberFunction( Polynomial1 + Polynomial2 ); the function will break when it tries to access the sum's ptr[] data member and the debugger will return: Unhandled exception at 0x543B6D46 (msvcp110d.dll) in SchoolProject.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.

I have no idea why it's doing this, I'm open to any suggestions.

Edit:

The answer is indeed that I needed to explicitly define a copy constructor and overloaded assignment operator in order to properly copy and assign objects. My overloaded operator+ was calling on the implicit copy constructor and operator=, which were not capable of copying the information pointed to by my *ptr data member. I have edited the text of my question to remove unrelated information.

  • You did not provide a user-defined copy constructor and assignment operator for your `Polynomial` class. You wouldn't need these functions if you used a plain `std::string` instead of a pointer to one as one of your members. Oh, and the problem has nothing to do with operator overloading -- it has everything to do with your `Polynomial` class not having correct copy semantics. You must have skipped over that chapter in your book (how to properly copy objects). – PaulMcKenzie May 23 '16 at 16:44
  • Also, in the future, please form your questions in the form of a [mcve]. – Barry May 23 '16 at 16:46
  • Also, the quick and dirty answer to what is happening is that the destructor for `Polynomial` is being called more than once on the same pointer, the very same pointer that holds your data. Put a breakpoint in your Polynomial destructor, watch the value of `ptr` -- you will see that the destructor is called on the same `ptr` value multiple times, more importantly, the first time since that is where your data lies. – PaulMcKenzie May 23 '16 at 16:56
  • @PaulMcKenzie I apologizes for the mess my code is in. I did in fact experiment with an assignment constructor, but I found that I could get objects to copy without defining one, so I assumed it was unnecessary(<-n00b). I was struggling with how to include as little code as possible(my code may be getting a little out of hand), and in the process violated the minimal, complete, and verifiable example requirements Barry mentioned. – Keeper of Secrets May 23 '16 at 18:17
  • @Barry, I will try to make my question more complete and verifiable, my code is getting a bit out of hand, so it may be a while. – Keeper of Secrets May 23 '16 at 18:18
  • @KeeperofSecrets [Here](http://ideone.com/MJZ8z0) is your minimal example that shows the problem. Note that there is no `operator +` in that entire sample. All I did was attempt to copy `Polynomial` objects, and got a double free error. – PaulMcKenzie May 23 '16 at 18:32
  • @KeeperofSecrets Polynomial does need both copy constructor and assignment operator if your destructor is doing this { delete [] ptr; } or { delete ptr;}. You're returning a Polynomial by value, thus it is necessary that it copies correctly. What you posted won't copy correctly. But you can avoid all of this if you either declared a std::string object, of if it's some kind of dynamic array, a std::vector. Then there is no need for copy constructor, assignment operator, or destructor, as shown [here](http://ideone.com/BLXMUD) – PaulMcKenzie May 23 '16 at 18:38
  • I appreciate the time you guys put into setting me straight. Lo and behold, it works like a charm now that I have included the copy constructor and assignment operators. – Keeper of Secrets May 23 '16 at 19:39

0 Answers0