OK, worked it out, you need to add a "terminating NULL" - very C-like!
3x^2+2x^1-50x^0+0x^0
5x^1+25x^0+0x^0

Meaningful variable names would help a lot. a and b don't mean anything, but if you used something like term and power then that would make the code a lot clearer.

In situations like this I add a shedload of debugging printf statements showing clearly what's going on. In this case the crash is in polynomial::clear() after it's evaluated p+q (which isn't complete, by the way). So the crash probably has something to do with the temporary object the compiler creates. If you add a printf indicating what function you're in and what "this" is then that will shed a lot of light on what's happening. For example printf("polynomial ctor, this=%p\n",this); in the default constructor (polynomial::polynomial()) will show when a new object is created and where it is - the latter is useful in distinguishing different objects.

So with this debug added I get the following output:

polynomial ctor(), this=0012FE80
polynomial ctor(), this=0012FE74
polynomial ctor(), this=0012FE68
polynomial::read(), this=0012FE80
Enter terms, include the degree of each term:3x^2+2x^1-50x^0+0x^0
polynomial::print(), this=0012FE80
3x^2+2x^1-50x^0
polynomial::read(), this=0012FE74
Enter terms, include the degree of each term:5x^1+25x^0+0x^0
polynomial::print(), this=0012FE74
5x^1+25x^0
polynomial op+(const polynomial& p), this=0012FE74
polynomial ctor(), this=0012FD50
3 2 1
1
polynomial ctor(const polynomial& original=0012FD50), this=0012FD9C
polynomial::copy(const polynomial& original=0012FD50), this=0012FD9C
polynomial::clear(), this=0012FD9C

and further, stepping through the Visual Studio 2005 debugger, I find there is a problem on term* temp=first; where first is 0xcccccccc - which is the value VS2005 debugger puts in uninitialised memory, which could explain why you think the crash is in polynomial::operator=(const polynomial& p). ALWAYS ALWAYS ALWAYS initialise stuff you rely on! In this case all it takes is "first=0;" in BOTH polynomial constructors, to save you a lot of debugging time, because then the crash occurs when you dereference the NULL pointer, not some time after.

Oddly though when I add first=0 to the constructor polynomial::polynomial(const polynomial& original) and step through to the same line, first is not NULL as I expected but points to the 3x^2 term from the first polynomial. Not sure therefore what exactly has happened here. However the return sum in polynomial::operator+ returns an object whose first is NULL.

How odd. With first uninitialised, the program crashes out because it ends up pointing at something undefined. With first initialised to NULL it seems to work OK (except as already mentioned that operator+ is incomplete). The complete output from my debug version of the program then is:

polynomial ctor(), this=0012FE80
polynomial ctor(), this=0012FE74
polynomial ctor(), this=0012FE68
polynomial::read(), this=0012FE80
Enter terms, include the degree of each term:3x^2+2x^1-50x^0+0x^0
polynomial::print(), this=0012FE80
3x^2+2x^1-50x^0
polynomial::read(), this=0012FE74
Enter terms, include the degree of each term:5x^1+25x^0+0x^0
polynomial::print(), this=0012FE74
5x^1+25x^0
polynomial op+(const polynomial& p), this=0012FE74
polynomial ctor(), this=0012FD50
3 2 1
1
polynomial ctor(const polynomial& original=0012FD50), this=0012FD9C
polynomial::copy(const polynomial& original=0012FD50), this=0012FD9C
polynomial dtor, this=0012FD50
polynomial::clear(), this=0012FD50
polynomial op=(const polynomial& p), this=0012FD9C
2
3
4
polynomial dtor, this=0012FD9C
polynomial::clear(), this=0012FD9C
polynomial::print(), this=0012FE68
3x^2

If you take the debug printf statement approach, note you have to take the address of anything passed in by reference, for example printf("polynomial::copy(const polynomial& original=%p), this=%p\n",&original,this); Here original is passed by reference rather than by name, and in C++ passing by reference uses the same semantics as passing by value, hence the need to take the address as in &original.

Last edited by xpi0t0s; 17Aug2008 at 17:29.. Reason: convert smilies back to proper text, in code :( really means colon open bracket