Overloaded Operator and arguments passed by reference

Just spent about 12 hours sorting a huge program out for an assignment.
One of the things that held me up was getting a runtime error whenever the arguments were passed by value whenever I tried to overload the + or - or << operators. When the parameters were passed by reference everything worked out just fine. Why is that? I'm proud that I just so happened to figure it out, but I want to understand.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void operator+(const Polynomial &poly1, const Polynomial &poly2)
{
Polynomial solution;
if (poly1.terms > poly2.terms)
  solution.terms = poly1.terms;
else
  solution.terms = poly2.terms;
solution.coeff = new int[solution.terms];

for (int i = 0; i<solution.terms; i++)
  solution.coeff[i] = poly1.coeff[i] + poly2.coeff[i];
solution.getPoly(); //function prints the polynomial
	}
  
If you pass by value the arguments will be copied. Can the Polynomial class handle being copied? If not, that could explain why you got a runtime error when it happened.

Your operator+ doesn't do what one normally expects operator+ to do. The normal behaviour is to return the sum of the two operands, without printing anything, so that you can do things like:

 
Polynomial polySum = poly1 + poly2;

Are you sure you don't want to return the solution instead of printing it? For that to work you must first fix so that Polynomial can be copied properly.
Last edited on
The reason why I didn't default to returning "polysum" was because my overloaded << operator was also spitting out a runtime error. However utilizing an overloaded << operator was not a requirement of the assignment so I utilized something that would work for what I needed.

What sort of things would make my polynomial class uncopiable? Will post code if needed.
What sort of things would make my polynomial class uncopiable?

It's not a matter of being uncopiable, but rather that the compiler provided copy constructor won't work correctly.

You haven't shown the declaration for your class, so I'm making a few assumptions.
1) You don't have a copy constructor.
2) solution.coeff is a pointer to dynamically allocated memory.
3) Your destructor deallocates the dynamically allocated memory.

Remember that if you don't provide a copy constructor, the compiler will provide one that does a shallow copy. A shallow copy of a pointer to dynamically allocated memory means that you now have two instances pointing to the same memory. When you delete the dynamically allocated memory in one instance, the pointer in the other instance is not changed and now points to memory that has already been deallocated. Then when the other instance tries to delete that same memory, you get a trap.

In the snippet at line 8 in your OP, what happens to the previous memory allocated to coeff? You have a memory leak here.


Last edited on
Also at line 11 of your original post, what happens if poly1.terms = 3 and poly2.terms = 6? Won't you end up trying to copy 6 coefficients from poly1?

I urge you to post your code. It sounds like you have some problems that are preventing you from writing the code the way it should be written.
Topic archived. No new replies allowed.