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.