Polynomial Array Overloaded Operator

I have the following assignment:
Develop the class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term:
2x^4
has the coefficient of 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities:
Overload the addition operator (+) to add two Polynomials.
Overload the subtraction operator (−) to subtract two Polynomials

I tried really hard to make it work, but I just don't have enough knowledge. The compiler doesn't help me too much, since the only error I receive is:
103: parse error at end of output (the last line)
I don't know how to add the get() as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  #include<iostream>
#include<cmath>

using namespace std;

const int MAXSIZE=10;

	class Polynomial{
private:
int x;
int Term;
int termsArray[MAXSIZE];
public:
Polynomial()
{
 x=0;
 Term=1;
 for (int i=0; i<MAXSIZE; i++)
 termsArray[i]=0;
}
~Polynomial();

void setPolynomial()
{
 int exp;
 int xValue;
 int nTerm;
 double coeff;
cout<<"Enter the value of x: ";
 cin>>xValue;
 x = xValue;
 cout<<endl<<"Enter the number of terms: ";
 cin>>nTerm;
 Term = nTerm;
 for(int i = 0 ; i < Term ; ++i )
 {
 cout<<endl<<"***** TERM # "<<i+1<<"******";
 cout<<endl<<"Enter the exponent: ";
 cin>>exp;
 cout<<endl<<"Enter the coefficient: ";
 cin>>coeff;
 termsArray[exp] = coeff;
 }
 }
 
void print()
{
 
 cout<<endl<<"The polynomial is :"<<endl;
 for(int exp = MAXSIZE-1 ; exp >= 0 ; --exp)
 {
 if(termsArray[exp] != 0)
zero)
 if(exp == 0)
 cout<<" + "<<termsArray[exp];
 else 
 {
 if(!firstTermWritten)

 {
 cout<<termsArray[exp]<<"x"<<exp;
 firstTermWritten = true;
 }
else 

 cout<<" + "<<termsArray[exp]<<"x"<<exp;
 }
 }
 cout<<endl;
 }

 
Polynomial operator+(Polynomial p2)
{
 Polynomial NewPol;
 for( int exp = 0 ; exp < MAXSIZE ; ++exp)
 NewPol.termsArray[exp] = termsArray[exp] + p2.termsArray[exp] ;
 return NewPol;
 }
 
 Polynomial operator-(Polynomial p2)
{
 Polynomial NewPol;
 for( int exp = 0 ; exp < MAXSIZE ; ++exp)
 NewPol.termsArray[exp] = termsArray[exp] - p2.termsArray[exp] ;
 return NewPol;
 }
 
 int main()
{ 
Polynomial Pol1, Pol2, Pol3;
Pol1.setPolynomial();

cout << "\nSet the second polynomial.\n";
 Pol2.setPolynomial();
 Pol3=Pol1+Pol2;
 Pol3.print();
 
 
 Pol3=Pol1-Pol2;
 Pol3.print();
}
Line 21: You never define your destructor.

Line 28: coeff is a double. Line 42: You're storing it in an int. Did you intend to truncate it?

Line 53: WTF is this?

Lines 58,62: firstTermWritten is never defined.

Line 88: I see no }; ending the class declaration. This is confusing the compiler.

Your code BADLY needs some reasonable indentation.

Last edited on
I fixed it a little bit. I just don't know how to define the destructor.
And I still get the same error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<cmath>

using namespace std;

const int MAXSIZE=10;

class Polynomial
{
private:
	int x;
	int Term;
	int termsArray[MAXSIZE];
public:
	Polynomial()
	{
 	x=0;
 	Term=1;
 	for (int i=0; i<MAXSIZE; i++)
 	termsArray[i]=0;
	}
	~Polynomial();

	void setPolynomial()
	{
 	int exp;
 	int xValue;
 	int nTerm;
 	int coeff;
	cout<<"Enter the value of x: ";
 	cin>>xValue;
 	x = xValue;
 	cout<<endl<<"Enter the number of terms: ";
 	cin>>nTerm;
 	Term = nTerm;
 	for(int i = 0 ; i < Term ; ++i )
 	{
 	cout<<endl<<"TERM # "<<i+1;
 	cout<<endl<<"Enter the exponent: ";
 	cin>>exp;
 	cout<<endl<<"Enter the coefficient: ";
 	cin>>coeff;
 	termsArray[exp] = coeff;
	}
	}

	Polynomial operator+(Polynomial P2)
	{
 	Polynomial NewPol1;
	for( int exp = 0 ; exp < MAXSIZE ; ++exp)
 	NewPol1.termsArray[exp] = termsArray[exp] + P2.termsArray[exp] ;
 	return NewPol;
 	}
 
	Polynomial operator-(Polynomial P2)
	{
 	Polynomial NewPol2;
 	for( int exp = 0 ; exp < MAXSIZE ; ++exp)
 	NewPol2.termsArray[exp] = termsArray[exp] - P2.termsArray[exp] ;
 	return NewPol;
};
 
 int main()
{ 
	cout << "The program will ask you to enter two polynomial and will add them";
	cout << "\n and substract the first one from the second one. " << endl;
	Polynomial Pol1, Pol2, Pol3, Pol4;
	cout << "\nEnter the parameter the first polynomial.\n";
	Pol1.setPolynomial();

	cout << "\nEnter the parameters the second polynomial.\n";
	Pol2.setPolynomial();
 	Pol3 = Pol1+Pol2;
 	cout << Pol3 << endl;
 
	Pol3 = Pol1-Pol2;
 	cout << Pol4 << endl;
  
}
I just don't know how to define the destructor.

Your destructor doesn't have to do anything, so an empty destructor is just fine.
1
2
~Polynomial()
{}

Or you could simply omit it and let the compiler provide a default destructor.

And I still get the same error.

When the compiler tells you you have a parse error at eof, that's usually an indication you have unpaired parens or braces.

Line 60: Where's your close brace for operator - ?



Last edited on
Thank you. I fixed that and now it is a disaster.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<cmath>

using namespace std;

const int MAXSIZE=10;

class Polynomial
{
private:
	int exp;
	int Term;
	int termsArray[MAXSIZE];
public:
	Polynomial()
	{
 
 	Term=1;
 	for (int i=0; i<MAXSIZE; i++)
 	termsArray[i]=0;
	}
	~Polynomial();

	void setPolynomial()
	{
 	int exp;
 	int nTerm;
 	int coeff;
	
 	cout<<endl<<"Enter the number of terms: ";
 	cin>>nTerm;
 	Term = nTerm;
 	for(int i = 0 ; i < Term ; ++i )
 	{
 	cout<<endl<<"TERM # "<< i+1;
 	cout<<endl<<"Enter the exponent: ";
 	cin>>exp;
 	cout<<endl<<"Enter the coefficient: ";
 	cin>>coeff;
 	termsArray[exp] = coeff;
	}
	}

	Polynomial operator+(Polynomial P1)
	{
 	Polynomial NewPol1;
	for( int exp = 0 ; exp < MAXSIZE ; ++exp)
 	NewPol1.termsArray[exp] = termsArray[exp] + P1.termsArray[exp] ;
 	return NewPol1;
 	}
 
	Polynomial operator-(Polynomial P2)
	{
 	Polynomial NewPol2;
 	for( int exp = 0 ; exp < MAXSIZE ; ++exp)
 	NewPol2.termsArray[exp] = termsArray[exp] - P2.termsArray[exp] ;
 	return NewPol2;}
};
 
 int main()
{ 
	cout << "The program will ask you to enter two polynomials and will add them";
	cout << "\n and substract the first one from the second one. " << endl;
	Polynomial Pol1, Pol2, Pol3, Pol4;
	cout << "\nEnter the parameters the first polynomial.\n";
	Pol1.setPolynomial();

	cout << "\nEnter the parameters the second polynomial.\n";
	Pol2.setPolynomial();
 	Pol3 = Pol1+Pol2;
 	cout << Pol3 << endl;
 
	Pol3 = Pol1-Pol2;
 	cout << Pol4 << endl;
  


There are the endless errors:
Checking file dependency...

\c++\polynomial\PolynomialArray.cpp:76: no match for `_IO_ostream_withassign & << Polynomial &'
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:77: candidates are: class ostream & ostream::operator <<(char)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:78: class ostream & ostream::operator <<(unsigned char)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:79: class ostream & ostream::operator <<(signed char)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:80: class ostream & ostream::operator <<(const char *)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:82: class ostream & ostream::operator <<(const unsigned char *)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:84: class ostream & ostream::operator <<(const signed char *)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:85: class ostream & ostream::operator <<(const void *)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:86: class ostream & ostream::operator <<(int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:87: class ostream & ostream::operator <<(unsigned int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:88: class ostream & ostream::operator <<(long int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:89: class ostream & ostream::operator <<(long unsigned int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:91: class ostream & ostream::operator <<(long long int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:92: class ostream & ostream::operator <<(long long unsigned int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:94: class ostream & ostream::operator <<(short int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:95: class ostream & ostream::operator <<(short unsigned int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:97: class ostream & ostream::operator <<(bool)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:99: class ostream & ostream::operator <<(double)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:100: class ostream & ostream::operator <<(float)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:104: class ostream & ostream::operator <<(long double)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:106: class ostream & ostream::operator <<(ostream & (*)(ostream &))
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:107: class ostream & ostream::operator <<(ios & (*)(ios &))
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:108: class ostream & ostream::operator <<(streambuf *)
c++\polynomial\PolynomialArray.cpp:79: no match for `_IO_ostream_withassign & << Polynomial &'
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:77: candidates are: class ostream & ostream::operator <<(char)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:78: class ostream & ostream::operator <<(unsigned char)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:79: class ostream & ostream::operator <<(signed char)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:80: class ostream & ostream::operator <<(const char *)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:82: class ostream & ostream::operator <<(const unsigned char *)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:84: class ostream & ostream::operator <<(const signed char *)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:85: class ostream & ostream::operator <<(const void *)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:86: class ostream & ostream::operator <<(int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:87: class ostream & ostream::operator <<(unsigned int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:88: class ostream & ostream::operator <<(long int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:89: class ostream & ostream::operator <<(long unsigned int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:91: class ostream & ostream::operator <<(long long int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:92: class ostream & ostream::operator <<(long long unsigned int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:94: class ostream & ostream::operator <<(short int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:95: class ostream & ostream::operator <<(short unsigned int)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:97: class ostream & ostream::operator <<(bool)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:99: class ostream & ostream::operator <<(double)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:100: class ostream & ostream::operator <<(float)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:104: class ostream & ostream::operator <<(long double)
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:106: class ostream & ostream::operator <<(ostream & (*)(ostream &))
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:107: class ostream & ostream::operator <<(ios & (*)(ios &))
[Error] C:\PROGRA~2\C-FREE~1\mingw32\Include\G__~1\iostream.h:108: class ostream & ostream::operator <<(streambuf *)

Complete Make PolynomialArray: 46 error(s), 0 warning(s)


Line 71,74: You're trying to cout your Polynominal class, but you have not overloaded the << operator. The compiler has no clue how to cout your class.
I don't know how to do that, can you give me an example or some more explanations?
Operator overloading means that you tell the class how to use "<<" or "+" to print objects or to add two objects together.

Look at this:
http://www.tutorialspoint.com/cplusplus/cpp_overloading.htm

I just have some notes about your code,

To make it easy for us to read, please, for the love of god, don't do this:
1
2
for(......)
bla bla


Use the brackets to start and end your for loops!

Also, how don't you know how to do operator overloading and your code has already overloaded two operators - and +?
Thank you for your info. I will take some more time to read about overloading.
I changed it. The destructor was getting an error, so I removed it and it compiled. But my operator+ and operator- are not working right and the program just stops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<cmath>

using namespace std;

const int MAXSIZE=10;

class Polynomial
{
private:
	int exp;
	int Term;
	int termsArray[MAXSIZE];
public:
	Polynomial()
	{
  		Term=1;
 		for (int i=0; i<MAXSIZE; i++)
 		termsArray[i]=0;
	}
	

	void setPolynomial()
	{
 		int exp;
 		int nTerm;
 		int coeff;
	
 		cout<<endl<<"Enter the number of terms: ";
 		cin>>nTerm;
 		Term = nTerm;
 		for(int i = 0 ; i < Term ; ++i )
 		{
 			cout<<endl<<"TERM # "<< i+1;
 			cout<<endl<<"Enter the exponent: ";
 			cin>>exp;
 			cout<<endl<<"Enter the coefficient: ";
 			cin>>coeff;
 			termsArray[exp] = coeff;
		}
	}

	Polynomial operator+(Polynomial P)
	{
 		Polynomial NewPol;
		for( int exp = 0 ; exp < MAXSIZE ; ++exp)
 		NewPol.termsArray[exp] = termsArray[exp] + P.termsArray[exp] ;
 		return NewPol;
 	}
 
	Polynomial operator-(Polynomial P)
	{
 		Polynomial NewPol;
 		for( int exp = 0 ; exp < MAXSIZE ; ++exp)
 		{NewPol.termsArray[exp] = termsArray[exp] - P.termsArray[exp] ;
 		return NewPol;}}
 	
  	friend ostream &operator<<( ostream &output, const Polynomial &Pol)                                 
   	{ 
		
  		output << Pol;
    	return output;            
    }

};
 
 int main()
{ 
	cout << "The program will ask you to enter two polynomials and will add them";
	cout << "\n and substract the second one from the first one. " << endl;
	Polynomial Pol1;
	Polynomial Pol2;
	Polynomial Pol3;
	Polynomial Pol4;
	cout << "\nEnter the parameters of the first polynomial.\n";
	Pol1.setPolynomial();

	cout << "\nEnter the parameters of the second polynomial.\n";
	Pol2.setPolynomial();
 	Pol3 = Pol1+Pol2;
 	cout << "The sum of the two polynomialas is: " << Pol3 << endl;
 
	Pol4 = Pol1-Pol2;
 	cout << "The second polynomial substracted from the first one is: " << Pol4 << endl;
  
}
Kudos on overloading the >> operator, however, you still have a problem.

Line 61: This is just going to cause an infinite recursion. The compiler still doesn't know how to format a Polynominal instance. The << operator is just going to call itself repeatedly.
In your first post (line 46), you had some very explicit logic for printing a Polynominal.

Suggestion: Resurrect Print() from your original post, then change line 61 as follows:
 
  Pol.Print (output);

Note that I changed Print to take an ostream argument. Replace all occurrences of cout inside Print() with output.

Line 56: Your return statement is inside your for loop. I don't think that's what you want. Had you aligned your closing braces with the corresponding opening braces, you would have noticed this.

Line 46, 54: It's a poor practice to name local variables the same as member variables of your class. The compiler knows the difference (the local variable takes precedence), but it's confusing to the reader.

Line 11: I'm wondering why exp is even a member variable. You don't seem to ever use it.
Last edited on
Thank you,
You helped me a lot. I need to fill some major gaps in me knowledge and come back to this program.
Don't give up. You've very close.
Mostly simple mistakes that need to be overcome.
Topic archived. No new replies allowed.