If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

HELP Linked List and operation overloading

Hello. I am having a bit of difficulty with implementing an object oriented program that uses both linked lists and operator overloading and would like some help. The program calls for adding and multiplying polynomials together, with each single polynomial being represented as a node of a linked list (which is further a data member of an object of a class I have defined to implement this program). For example:

polynomial A will be: 3x^4 // 1 node of a linked list
polynomial B will be: 5x^2 // 1 node of a linked list
polynomial C will be blank for the time being. // empty list

Now, I need to use operator overloading so that this following line of code can be implemented:

C = A + B;

C should now be: 3x^4 + 5x^2.

The checklist of which parts of my code that work:

constructor works;
copy constructor works;
destructor works;
operator= works;
print function needs work but i can worry about that later;
operator+ HELP: HIGHLIGHTED IN RED
operator* work on later

The purpose of the copy constructor is to make an exact copy of the object that is passed in. Semi-copies, fake copies, bogus copies, are not to be made by the copy constructor. The reason is that the compiler itself can call the copy constructor for various reasons. If your copy constructor has side-effects where the copy is not a real copy, then all bets are off as to how the program will behave.

Your copy constructor (and your assignment operator) uses an if() statement that checks the condition of local variables. This to me gives off all sorts of red flags.

What you're supposed to do is simply take the object that is passed in to the function, and blindly (yes, blindly) just copy the members of that object into this. No if() statements, no checking if the object passed in has a certain characteristic, nothing. Just take the object passed in, and assign this the value of the passed-in object's members. If you want to have a function that takes an object and does something that falls outside of these limitation, then write a separate function and call it by a different name, but don't use/abuse the copy-assignment operations for this purpose.

If you don't do that you end up with the fake or semi-copies, and code like this now becomes suspect:

Code:

polyType polyType::operator+(const polyType &rhs) const

Your copy constructor better make a valid copy, or else all of this is trash. You are returning a copy, and the copy constructor needs to be coded correctly.

Edit: If your class is supposed to be a linked list, then you should have had a simple "addNode" function that simply adds a node to the end of the list. I don't see one coded. If you coded it, and also a simple "clear" function that removes all the nodes, the copy constructor and assignment operator become trivial functions that just start from the head of rhs and calls "add" for each node in rhs.

What is the purpose of the loop? More to the point, why are you just assigning re.polyPtr->exp and re.polyPtr->coef in a loop, overwriting the previous values assigned to it? You might as well just looped to the end and assign the last known values to re.polyPtr->exp and re.polyPtr->coef.

Re: HELP Linked List and operation overloading

Originally Posted by aces237

Yes, I have used the debugger.

And where does the program goes counter to what you expect? If you used the debugger, what variable(s) are not set correctly, program flow doesn't flow correctly, functions, are not being called or called incorrectly, etc.?

Going back to your copy constructor, where exactly do you delete "newP->next"? I don't see where newP is assigned to anything, and you just exit the function with this memory allocated, causing a leak. As a matter of fact, your destructor for polyton is supposed to go through the nodes and delete them, but I don't see this coded anywhere. Your destructor just simply deletes that single pointer.

d.print(); // should print out 7x^6 + 5x^4 + 3x^2, but it will only print out: 3x^2 + 7x^6

So did you fix the print() function so that it doesn't allocate a pointer to memory and then immediately overwrites it?

Re: HELP Linked List and operation overloading

Going back to your original question and assuming you're sorted out the issues raised by Paul above. You can't just do an 'add' of linked lists by a single statement as you are trying. You need to create a new list with individual nodes for each of the different exponents in the two lists and where the two lists have an exponent in common then you need to add the coefficients for the exponent in the new list.

Also your operator= method is also leaking memory as again you are just deleting the memory pointed to by polyPtr and not the memory used for all of its links. To delete a linked list, you need to walk the links and delete each node. Adding and removing nodes and deleting the linked list should be methods of the list class. Your polyType class should not have and not need any knowledge of the internal workings of list class. So rather than just have a struct trying to act like a poor class with a constructor, make this a proper class with add, remove, delete, copy methods etc. These methods are then used by the polyType class so polyType doesn't deal with memory as it does now.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.