I’m trying to overload the += operator in my program. It consists of a polynomial class that includes an int degree (the highest degree of the polynomial) and a Node* Poly (a pointer to a node)
struct Node{ int coefficient;
Node* next; };
that is my Node struct, I am overloading in the Polynomial class though.
Polynomial& Polynomial::operator +=(const Polynomial& rP){
Polynomial copyRP(rP);
poly->coefficient = poly->coefficient + copyRP.poly->coefficient;
poly = poly->next; //if I take this and
copyRP.poly = copyRP.poly->next; //this away, it runs fine
//with it however the program compiles but doesnt work
return *this;
}
The node contains the list of the coefficients backwards, if that matters. For example 3x^2+2x+5 is stored in the node as 5->2->3 and has a degree of 2.
Try this:
No linked lists, no manual memory management, you can easily change it to another numeric type.