toogreat4u 127 Report post Posted March 12, 2009 I am trying to add two Polynomails together using the overloaded operator and as far as I can tell my code should run correctly but its blowing up after the operator is called in the driver program. So here is the code that is important. Polynomial operator +(const Polynomial& p, const Polynomial& p2) { Polynomial temp; PolyPtr bigger; PolyPtr smaller; int coef; int power; if(p.head->power > p2.head->power) { bigger = p.head; smaller = p2.head; } else { bigger = p2.head; smaller = p.head; } int count = 0; while(bigger != NULL || smaller != NULL) // only kill when both are null kill { if(bigger->power == smaller->power) // we have equal powers add the coefficients up { coef = bigger->coefficent + smaller->coefficent; // adding coefficients power = bigger->power; // power can be either but chose bigger temp.insert(coef,power); // insert it into the newly forming polynomial bigger = bigger->link; // move down the bigger poly } else if(bigger->power > smaller->power) // when the bigger power is larger { coef = bigger->coefficent; // only take that power power = bigger->power; // only take that coefficient temp.insert(coef,power); // insert it into new polynomial bigger = bigger->link; // move down bigger } else if(bigger->power < smaller->power) // when bigger is now smaller { coef = smaller->coefficent; // only take that coefficient power = smaller->power; // only take that power temp.insert(coef,power); // insert it into new polynomial smaller = smaller->link; // move down smaller } else if(smaller == NULL) // no more left in smaller { coef = bigger->coefficent; // only one available so take it power = bigger->power; // only one available so take it temp.insert(coef,power); // insert it into new polynomial bigger = bigger->link; // move down the bigger } else if(bigger == NULL) // no more left in bigger { coef = smaller->coefficent; // only one available so take it power = smaller->power; // only one available so take it temp.insert(coef,power); // insert into new polynomial smaller = smaller->link; // move down smaller } } return temp; } The insertion is working properly but for clearity here is the insertion operation. void Polynomial::insert(int c, int p) { if(head == NULL) { head = new PolyNode; head->coefficent = c; head->power = p; head->link = NULL; tail = head; } else { PolyPtr temp = new PolyNode; temp->coefficent = c; temp->power = p; temp->link = NULL; tail->link = temp; tail = temp; } } It's basically a queue formatted linked list. I am not sure as to why this is not working so any suggestions would be great. Thanks. 0 Share this post Link to post Share on other sites
toogreat4u 127 Report post Posted March 12, 2009 I found a small mistake in my code but still didnt fix it, so I am still looking for help also I cleaned it up a little bit so here is the new overloaded operator functionPolynomial operator +(const Polynomial& p, const Polynomial& p2){ Polynomial temp; PolyPtr first = p.head; PolyPtr second = p2.head; int coef; int power; while(first != NULL || second != NULL) { if(first != NULL && second != NULL) // only if the first isnt null { if(first->power > second->power) // first is bigger { coef = first->coefficent; // only option is coef power = first->power; // only option is power //cout << coef << endl; //cout << power << endl; temp.insert(coef, power); // insert first into new poly first = first->link; // move down first } else if(first->power == second->power) // if both powers are equal { coef = first->coefficent + second->coefficent; power = first->power; temp.insert(coef, power); //cout << coef << endl; //cout << power << endl; first = first->link; second = second->link; } else if(first->power < second->power) // second is bigger { coef = second->coefficent; power = second->power; temp.insert(coef, power); //cout << coef << endl; //cout << power << endl; second = second->link; } } else if(first != NULL && second == NULL) { coef = first->coefficent; power = first->power; temp.insert(coef, power); //cout << coef << endl; //cout << power << endl; first = first->link; } else if(first == NULL && second != NULL) { coef = second->coefficent; power = second->power; temp.insert(coef,power); //cout << coef << endl; //cout << power << endl; second = second->link; } } return temp;}Still can not figure out why it is not working, as you can see I tested to see if the correct coef and power were being passed around and that much is definitely working so it has something to do with how I am using first and second but I am not sure of what exaclty. Thanks for any help. 0 Share this post Link to post Share on other sites
livengood 144 Report post Posted March 12, 2009 Under the assumption that the code is inside an object for binary operators you only use one parameter as the this pointer is implicitly the first object.Here's an example:Vector3 operator+ (const Vector3& vec){ return Vector3(x + vec.x, y + vec.y, z + vec.z);}You can see from this that the x,y,z are coming from the this object.In the code like this:Vector3 vec1;Vector3 vec2;Vector3 result;result = vec1 + vec2;vec1 is the this pointer, vec2 fills in the operator+ parameter and the return Vector3 is then fed to the operator= which returns Vector3 result.Hope that helps. 0 Share this post Link to post Share on other sites