Problems with overloading operator

I am getting an error on line 104. This line is suppose to add the coefficients of two polynomials.

p[term].coefficient = a1.p[i].coefficient + a2.p[j]coefficient; //error: invalid operands to binary expression ('int' and 'polynomial::poly')

I am also getting an error on the overloading operator-() function on line 236. This function is suppose to negate the polynomial setting it to -1.

p[i].coefficient *= -coefficient; //reference to nonstatic member function must be called?

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//this is the header file
#ifndef POLYNOMIAL_H
#define	POLYNOMIAL_H
#include <cstdlib>
#include <iostream>



using namespace std;

const int MAX_TERMS = 10;

class polynomial{

public:
    //Default constructor
    polynomial();
    
    //gets the user-input for polynomial
    void getData();
    
    //constructor with user-input parameters
    explicit polynomial(int& x);
    
    //accessor for the degree
    int degree() const;
    
    //returns the coefficient of the x-power term
    int coefficient(int& power) const;
    
    //replaces the coefficient of the x-power term
    void changeCoefficient(int& newCoefficient, int& power);
    
    //multiplies a polynomial by a scalar variable
    polynomial scalarMulitply(int& scalar);
    
    //adds two polynomials
    void addPolynomials(polynomial& P1, polynomial& P2);
    
    //prints out polynomial
    void printPolynomial() const;
    
    //overloading operator to divide a polynomial by a scalar variable
    polynomial operator/(int& scalar);
    
    //overloading operator to negate a polynomial
    polynomial operator-();
    
    //overloading operator to output a polynomial on an output stream
    friend ostream& operator<<(ostream& outStream, polynomial& P1);
    

//struct for polynomial terms
struct poly{
    int coefficient;
    int power;
};    
private:
    int num_terms;
    int new_terms;
    
    poly p[MAX_TERMS];
    
};


#endif	/* POLYNOMIAL_H */
************************************************************
//This is the .cpp file
#include "polynomial.h"
//#include "polynomial.h"
#include <iostream>
#include <string>

using namespace std;

//Default constructor

polynomial::polynomial() {
    num_terms = 1;
    int i = 0;

    for (i = 0; i < num_terms; i++) {
        p[i].coefficient = 0;
        p[i].power = 0;
    }

}

//get data from user

void polynomial::getData() {
    int i =0;
    for(i=0; i<num_terms; i++){
        cout <<" Enter the coefficient (Please include '+' or '-' before integer)";
        cin >> p[i].coefficient;
        cout <<" Enter the power";
        cin >> p[i].power;
    }
}

//Polynomial constructor with user-input parameters

polynomial::polynomial(int& x) {
    num_terms = x;
    int i = 0;
    for (i = 0; i < num_terms; i++) {
        p[i].coefficient = 0;
        p[i].power = 0;
    }

}

//returns degree of a polynomial

int polynomial::degree() const {
    int degree = 0;
    int i = 0;
    for (i = 0; i < num_terms; i++) {
        if (degree <= p[i].power) {
            degree = p[i].power;
        }
    }
}

//returns the coefficient of the x-power term

int polynomial::coefficient(int& power) const {
    int i = 0;
    for (i = 0; i < num_terms; i++) {
        if (p[i].power == power) {
            return p[i].coefficient;
        }
    }
}

//replaces the coefficient of the x=power term with newCoefficient

void polynomial::changeCoefficient(int& newCoefficient, int& power) {
    int i = 0;
    for (i = 0; i < num_terms; i++) {
        if (p[i].power == power) {
            p[i].coefficient = newCoefficient;
        }
    }

}

//Multiplies a polynomial by a scalar variable

polynomial polynomial::scalarMulitply(int& scalar) {
    polynomial p1(num_terms);
    for (int i = 0; i < num_terms; i++) {
        p1.p[i].coefficient = p[i].coefficient * scalar;
        p1.p[i].power = p[i].power;
    }

    return p1;
}

//Adds two polynomials

void polynomial::addPolynomials(polynomial& a1, polynomial& a2) {
    int term = 0;
    int i = 0;
    int j = 0;
    bool found = false;

    //loops through the first polynomial comparing terms from second poylynomial
    for (i = 0; i < a1.num_terms; i++) {
        for (j = 0; j < a2.num_terms; i++) {
            if (a1.p[i].power == a2.p[i].power) {
                p[term].coefficient = a1.p[i].coefficient + a2.p[j]coefficient;
                p[term].power = a1.p[i].power;
                term++;
                found = true;

            } else {
                found = false;
            }
        }

    }
    //iterate through second polynomial comparing terms from first polynomial
    for (i = 0; i < a2.num_terms; i++) {
        for (j = 0; j < a2.num_terms; i++) {
            if (a2.p[i].power == a1.p[j].power) {
                found = true;
                break;
            } else {
                found = false;
            }
        }
        if (!found) {
            p[term].coefficient = a2.p[i].coefficient;
            p[term].power = a2.p[i].power;
            term++;
        }
        found = true;
    }
}

// prints out a polynomial

void polynomial::printPolynomial() const {
    int i = 0;
    cout << " Your Polynomial: ";
    for(i=0; i< num_terms; i++){
        cout << p[i].coefficient << p[i].power << endl;
    }        
}


// Overloading operator to divide a polynomial by a scalar variable

polynomial polynomial::operator/(int& scalar) {
    int i = 0;
    polynomial p1(num_terms);

    if (scalar == 0) {
        cout << "Scalar cannot be 0";
    } else {
        for (i = 0; i < 10; i++) {
            p1.p[i].coefficient = p[i].coefficient / scalar;
            p1.p[i].power = p[i].power;
        }
    }
    return p1;
}

//Overloading operator to negate a polynomial

polynomial polynomial::operator-() {
    int i = 0;
    for (i = 0; i < num_terms; i++) {
        p[i].coefficient *= -coefficient;
    }
    return *this;
}

ostream &operator<<(ostream& outStream, polynomial& p1) {
    int i = 0;
    for (i = 0; i < p1.num_terms; i++) {
        if ((p1.p[i].coefficient > 0) && (i == 0)) {
            outStream << p1.p[i].coefficient << "x^" << p1.p[i].power << " ";
        } else if ((p1.p[i].coefficient > 0) && (i > 0)) {
            outStream << "+ " << p1.p[i].coefficient << "x^" << p1.p[i].power << " ";
        } else if ((p1.p[i].coefficient < 0) && (i == 0)) {
            outStream << p1.p[i].coefficient << "x^" << p1.p[i].power << " ";
        } else if ((p1.p[i].coefficient < 0) && (i > 0)) {
            outStream << "+ " << p1.p[i].coefficient << "x^" << p1.p[i].power << " ";
        }
    }
    outStream << endl << endl;

    return outStream;
}


Last edited on
you need to add #include "polynomial.h" to the top of your cpp file.
This looks wrong:
1
2
#include "ADT.h" // Not a problem since you have header guards but why the header is including itself?
#include "ADT.cpp" // You should not include .cpp files.  
the file name was "ADT". I'm guessing when I include the header file, it's the name of the class which is "polynomial" and NOT "ADT"?

Anyhow, I changed the file name from "ADT" to "polynomial" and it works.
Last edited on
Topic archived. No new replies allowed.