Hello!
I'm having a problem with overloading "*" operator and the "=" operator. The code in main goes like:
P3 = P1*P2;
The error I get:
/Volumes/LarodHD2/Documents/School/Polytechnic University/CECS2222/Polinomial/main.cpp:21:0 /Volumes/LarodHD2/Documents/School/Polytechnic University/CECS2222/Polinomial/main.cpp:21: error: no match for 'operator=' in 'P3 = Polinomial::operator*(Polinomial&)(((Polinomial&)(& P1)))'
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
class Polinomial
{
private:
Term *termArray;
int coef, exp, numTerms;
public:
Polinomial(int _numTerms){
numTerms = _numTerms;
termArray = new Term[_numTerms];
}
Polinomial(){
termArray = NULL;
numTerms = 0;
}
~Polinomial(){}
int getNumTerms(){return numTerms;}
void setNumTerms(int _numTerms) { numTerms = _numTerms;}
void printPolinomial(){
cout << endl;
for (int i = 0; i < numTerms; i++) {
cout << termArray[i].getCoef() << "X^" << termArray[i].getExp();
if (i < numTerms-1) {
cout << " + ";
}
} cout << endl;
}
void setPolinomial(){
for (int i = 0; i < numTerms; i++) {
cout << "Enter the coefficient for term " << i+1 << ": ";
cin >> coef;
termArray[i].setCoef(coef);
cout << "Enter the exponent for term " << i+1 << ": ";
cin >> exp;
termArray[i].setExp(exp);
coef = exp = 0;
}
}
void addTerm(Term &T){
Term *tempArray;
tempArray = new Term[numTerms+1];
for (int i = 0; i < numTerms; i++) {
tempArray[i] = termArray[i];
}
tempArray[numTerms] = T;
termArray = tempArray;
numTerms += 1;
}
Polinomial operator+(Polinomial &P){
int counter = 0;
for (int i = 0; i < P.getNumTerms(); i++)
{
for (int j = 0; j < numTerms; j++)
{
if (termArray[j].getExp() != P.termArray[i].getExp()) {
counter++;
}
else if (termArray[j].getExp() == P.termArray[i].getExp()) {
termArray[j].setCoef(termArray[j].getCoef() + P.termArray[i].getCoef());
}
}
if (counter == numTerms) {
addTerm(P.termArray[i]);
}
counter = 0;
}
return *this;
}
Polinomial operator+=(Polinomial &P){
int counter = 0;
for (int i = 0; i < P.getNumTerms(); i++)
{
for (int j = 0; j < numTerms; j++)
{
if (termArray[j].getExp() != P.termArray[i].getExp()) {
counter++;
}
else if (termArray[j].getExp() == P.termArray[i].getExp()) {
termArray[j].setCoef(termArray[j].getCoef() + P.termArray[i].getCoef());
}
}
if (counter == numTerms) {
addTerm(P.termArray[i]);
}
counter = 0;
}
return *this;
}
Polinomial operator-(Polinomial &P){
int counter = 0;
for (int i = 0; i < P.getNumTerms(); i++)
{
for (int j = 0; j < numTerms; j++)
{
if (termArray[j].getExp() != P.termArray[i].getExp()) {
counter++;
}
else if (termArray[j].getExp() == P.termArray[i].getExp()) {
termArray[j].setCoef(termArray[j].getCoef() - P.termArray[i].getCoef());
}
}
if (counter == numTerms) {
addTerm(P.termArray[i]);
}
counter = 0;
}
return *this;
}
Polinomial operator-=(Polinomial &P){
int counter = 0;
for (int i = 0; i < P.getNumTerms(); i++)
{
for (int j = 0; j < numTerms; j++)
{
if (termArray[j].getExp() != P.termArray[i].getExp()) {
counter++;
}
else if (termArray[j].getExp() == P.termArray[i].getExp()) {
termArray[j].setCoef(termArray[j].getCoef() - P.termArray[i].getCoef());
}
}
if (counter == numTerms) {
addTerm(P.termArray[i]);
}
counter = 0;
}
return *this;
}
Polinomial operator=(Polinomial &P){
delete [] termArray;
termArray = new Term[P.getNumTerms()];
for (int i = 0; i < P.getNumTerms(); i++) {
termArray[i] = P.termArray[i];
}
numTerms = P.getNumTerms();
return *this;
}
Polinomial operator*(Polinomial &P){
Polinomial temp;
if (numTerms >= P.getNumTerms()) {
temp.setNumTerms(numTerms);
temp.termArray = new Term[numTerms];
for (int i = 0; i < numTerms; i++) {
temp.termArray[i] = termArray[i]*P.termArray[i];
}
return *this;
}
else {
temp.setNumTerms(P.getNumTerms());
temp.termArray = new Term[P.getNumTerms()];
for (int j = 0; j < numTerms; j++) {
temp.termArray[j].setCoef(termArray[j].getCoef() * P.termArray[j].getCoef());
temp.termArray[j].setExp(termArray[j].getExp() + P.termArray[j].getExp());
}
for (int k = numTerms; k < P.getNumTerms(); k++) {
temp.addTerm(P.termArray[k]);
}
return *this;
}
}
Polinomial operator*=(Polinomial &P){
if (numTerms >= P.getNumTerms()) {
for (int i = 0; i < P.getNumTerms(); i++) {
termArray[i].setCoef(termArray[i].getCoef() * P.termArray[i].getCoef());
termArray[i].setExp(termArray[i].getExp() + P.termArray[i].getExp());
}
return *this;
}
else {
for (int j = 0; j < numTerms; j++) {
termArray[j].setCoef(termArray[j].getCoef() * P.termArray[j].getCoef());
termArray[j].setExp(termArray[j].getExp() + P.termArray[j].getExp());
}
for (int k = numTerms; k < P.getNumTerms(); k++) {
addTerm(P.termArray[k]);
}
return *this;
}
}
};
Here is my main:
#include <iostream>
#include "Polinomial.h"
using namespace std;
int main () {
int terms;
cout << "Enter the number of terms for first Polinomial: ";
cin >> terms;
Polinomial P1(terms);
P1.setPolinomial();
P1.printPolinomial();
terms = 0;
cout << "Enter the number of terms for second Polinomial: ";
cin >> terms;
Polinomial P2(terms);
P2.setPolinomial();
P2.printPolinomial();
While that fixes the implementation error, I should also note that the multiplication operation is mathematically incorrect. When I tested (3x^2 + 2x + 4)*(6x^2 + x +8) the program returned 18x^4 + 2x^2 + 32, when it should be 18x^4 + 15x^3 + 50x^2 + 20x + 32. Something to look into.