Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to make a class for polynomial functions. So one feature of this class should be to sum 2 polynomial function so I should overload + operator for the class. I wrote this code but I got error:


What's wrong in my code?

#include <iostream>
#include <conio.h>

using namespace std;

//Polynomial Class
class Polynomial
	int *factor;
	int *power;
	unsigned int n;

		n = 0;

		delete[] factor;
		delete[] power;

	void Sort();
	void swap(int &x, int &y);

	void MakePolynomial(unsigned int NumberofTerms);
	void Print();
	Polynomial operator +(Polynomial p);
	Polynomial operator -(Polynomial p);
	void Search();
	bool operator ==(Polynomial p);
	char *CalDerived();

void Polynomial::Sort()
	int last = (n + 1) - 2; 
	int isChanged = 1; 

	while (last >= 0 && isChanged) 
		isChanged = 0; 
		for (int k = 0; k <= last; k++)
			if (power[k] < power[k+1]) 
				swap(power[k], power[k+1]); 
				swap(factor[k], factor[k+1]); 
				isChanged = 1; 

void Polynomial::swap(int &x, int &y)
	int temp; 
	temp = x; 
	x = y; 
	y = temp; 

void Polynomial::MakePolynomial(unsigned int No)
	factor = new int[No];
	power = new int[No];

void Polynomial::Print()
	for(int i = 0; i < n; i++)
		if(factor[i] >= 0)
			cout << "+" << factor[i] << "x^" << power[i];
			cout << factor[i] << "x^" << power[i];

Polynomial Polynomial::operator +(Polynomial p)
	Polynomial temp;

	for(int i = 0; i < n; i++)
		temp.factor[i] = factor[i] + p.factor[i];
		temp.power[i] = power[i];
	return temp;
Polynomial Polynomial::operator -(Polynomial p)

void main()
	Polynomial p, p2, sum;

	p.factor[0] = 2;
	p.power[0] = 0;
	p.factor[1] = -4;
	p.power[1] = 1;
	p.factor[2] = 3;
	p.power[2] = 2;
	p.n = 3;

	cout << endl << "+\n";

	p2.factor[0] = 3;
	p2.power[0] = 0;
	p2.factor[1] = 4;
	p2.power[1] = 1;
	p2.factor[2] = 7;
	p2.power[2] = 2;
	p2.n = 3;

	cout << endl << "=\n";

	sum.n = 3;
	sum = p + p2;



CPallini 16-Jun-11 6:54am    
You should report the exact error message (which details the offending line., etc..)

Where and when do you get that error?

The only actual problem I can see is that you didn't provide an assignment operator, so the compiler will provide it's own, and that one will simply copy all members, including the pointers. This default assignment operator will be called in this line:
sum = p + p2;

The problem here is that now this will happen:
1. operator+ will create a new, temporary Polynomial object
2. The default assignment operator will copy all data from that temporary object to sum, including the pointers factor and power.
3. The temporary object will be destroyed, and therefore its arrays power and factor will be deleted.
4. Now sum.factor and sum.power point to invalid memory!
5. the call to sum.Print() will likely fail
6. Even if not, once the program exits, sum will be destructed, trying to delete[] power and factor, causing a runtime error.

Solution: Create your own assignment operator that actually copies the arrays rather than the pointers.

There's also another issue in the implementation of operator+: you are assuming that the two operands have (no more than) 3 terms, and that these terms have corresponding values for power. Either you drop power from your data structure entirely and just save your 'factor's in a predefined order, or else you must compare the power values and up only the corresponding ones, and you must consider the possibility that upon adding two polynomials with 3 factors each, the resulting polynomial might need to store up to 6 factors!
Share this answer
Other problems include your not initializing factor and power to 0 in the constructor making the following undefined:
    Polynomial p; // constructed
} // destructed, and most likely with an access violation
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900