I've been trying to code for this question, where I have to add and compare two polynomials. Issues I have been facing are, with the compare function, whether the two polynomials I enter are equal or not, I get an output saying they are equal. And with overloading the + operator, I get the std bad_alloc error.
Would really appreciate help with this! I'm using the g++ compiler and I have also tried this in codeblocks.
First of all, I would pass t by const& in both 72 and 84.
Next, in operator==, you need to make sure that the polynomials are the same size.
Next, in operator+, you need to make sure that only populated elements in the accessed polynomials are used. Don't go past the bounds. And if the polynomials are not the same size, you need to figure out if you need to skip elements at the beginning or at the end when adding them up (probably at the beginning).
Line 70 has problems. First of all, == is a comparison, not an assignment. Also, you should be adding d.a[i] = t.a[i] + a[i].
There's probably more, but that's enough for now.
Edit: one more--line 134 should just be int y = (r == f);.
You don't need the template function.
I'm not exactly sure shy you are getting a std::bad_alloc error. You should probably run your code through a debugger to find out when the bad_alloc occurs.
In my previous post, I mentioned line 70. My eyesight must be going bad because I really meant line 79 (it looked like 70 to my eyes). Since your code is failing in operator+(), that's a good place to start.