-2

My intentions of this code is to use overloaded operators for assignment, addition, subtraction and multiplication of polynomials. What I have done so far are the assignment, addition overloaded operators but I am receiving errors. - Thanks

    polynomial.cpp: In member function ‘Polynomial& Polynomial::operator+=(const Polynomial&)’:
polynomial.cpp:67:13: error: cannot convert ‘float*’ to ‘float’ in assignment
     *newArr = new float[Capacity];
             ^
polynomial.cpp: In function ‘const Polynomial operator+(const Polynomial&)’:
polynomial.cpp:81:3: error: invalid use of ‘this’ in non-member function
  *this += poly1;
   ^
polynomial.cpp:82:10: error: invalid use of ‘this’ in non-member function
  return *this;
          ^
polynomial.cpp: In member function ‘Polynomial& Polynomial::operator=(const Polynomial&)’:
polynomial.cpp:94:3: error: ‘newArr’ was not declared in this scope
  *newArr = new float[this->Capacity];
   ^

-

#ifndef POLY_H
#define POLY_H

#include <iostream>
#include <cstring>

using namespace std;

//Base Class---------------------------------------------
class Polynomial {
    private:
        //number of coefficients & exponents
        int NumberofTerms;
        //arrays
        float *Coefficient_arr;
        float *Exponent_arr;
        //size
        int Capacity;
    public:
        //default constructor
        Polynomial();
        //regular constructor
        Polynomial(int numterms, float coef[], float expnt[]);
        //deconstructor
        ~Polynomial();
        //Setters - Getters
        float *get_coefficient() { return Coefficient_arr; }
        float *get_exponent() { return Exponent_arr ; }
        float get_numberofTerms() { return NumberofTerms;}
        //Overload addition
        Polynomial& operator += ( const Polynomial& poly );
        const Polynomial operator + (const Polynomial& other) const;
        //Assignment operator
        Polynomial& operator=(const Polynomial& poly );
};
 #endif

-

#include <iostream>
#include <cstring>

#include "polynomial.h" //header file

using namespace std;
//----------------------------------------------
//default constructor
Polynomial::Polynomial(){
    NumberofTerms = 1;
    Coefficient_arr = new float[1];
    Exponent_arr = new float[1];
    Capacity = 1;
    Coefficient_arr[0] = 0;
    Exponent_arr[0] = 0;
}
//regular constructor
Polynomial::Polynomial( int numterms, float coef[], float expnt[]){
    NumberofTerms = numterms;
    Coefficient_arr = new float[numterms];
    Exponent_arr = new float[numterms];
    Capacity = numterms;
    for (int i = 0; i < numterms; ++i)
    {
        Exponent_arr[i] = expnt[i];
        Coefficient_arr[i] = coef[i];
    }
}
//Destructor
Polynomial::~Polynomial(){
    delete[] Coefficient_arr;
    delete[] Exponent_arr; 
}
//Addition Overload
Polynomial& 
Polynomial::operator += ( const Polynomial& poly ){
    bool found_coeff;
    for (int i = 0; i < NumberofTerms; ++i) //first array 
    {
        found_coeff = false;
        for (int j = 0; j < NumberofTerms; ++j)//second array 
        {
            if (poly.Exponent_arr[i] == this->Exponent_arr[j]) 
            {   
                this->Coefficient_arr[j] += poly.Coefficient_arr[i];
                found_coeff = true;
            }
        }
        if (found_coeff == false)
        {
            this->NumberofTerms+=1;
            if (this->NumberofTerms > this->Capacity)
            {
                //resize Coefficient array
                Capacity = Capacity*2; 
                float *newArr = new float[Capacity];
                memcpy( newArr, this->Coefficient_arr, this->Capacity * sizeof(float) );
                delete [] this->Coefficient_arr;
                this->Coefficient_arr = newArr;
                delete [] newArr;
                //resize exponent array
                *newArr = new float[Capacity];
                memcpy( newArr, this->Exponent_arr, Capacity * sizeof(float) );
                delete [] this->Exponent_arr;
                this->Exponent_arr = newArr;
                delete [] newArr;
                //add exp and coeff to end of arr
                this->Coefficient_arr[ NumberofTerms ] = (poly.Coefficient_arr[i]);
                this->Exponent_arr[ NumberofTerms ] = (poly.Exponent_arr[i]);
            }
        }
    }
    return *this;
}
const Polynomial operator + (const Polynomial& poly1){
    *this += poly1;
    return *this;
}
//Assignment Operator
Polynomial& 
Polynomial::operator=(const Polynomial& poly){
    if(this == &poly){
        return *this;
    }
    //Assign poly to _this_ for Number of terms & Capacity
    this->NumberofTerms = poly.NumberofTerms;
    this->Capacity = poly.Capacity;
    //assign poly to _this_ for Coefficient
    *newArr = new float[this->Capacity];
    memcpy( newArr, poly.Coefficient_arr, poly.Capacity * sizeof(float) );
    delete [] this->Coefficient_arr;
    this->Coefficient_arr = newArr;
    delete [] *newArr;
    //assign poly to _this_ for exponent
    *newArr = new float[Capacity];
    memcpy( newArr, poly.Exponent_arr, poly.Capacity * sizeof(float) );
    delete [] this->Exponent_arr;
    this->Exponent_arr = newArr;
    delete [] *newArr;
    return *this;   
}
Bigboy6
  • 109
  • 2
  • 3
  • 13
  • 1
    `this->Coefficient_arr = newArr; delete [] newArr;` - what a mess – SergeyA Apr 18 '16 at 15:51
  • Well I'd appreciate any help I can, not just criticism - Thanks! @SergeyA – Bigboy6 Apr 18 '16 at 16:01
  • I'd think about changing the design in the first place. 'Polynomial' class doesn't need two arrays (for coefficients and exponents). The exponent array only adds some confusion (i.e. two polynomials `x+x` and `2*x` seem to be different in this implementation). So maybe just one array for coefficients (where `coef_arr[i]` would be the coefficient at `x^j`. Or a vector instead of an array. Or maybe a map? – ptrj Apr 18 '16 at 16:07

2 Answers2

0

Yes, you are getting errors. You are trying to assign new float[Capacity] (of type float *) to *newArr (of type float &). You use this in non-member function (which is not even defined there). Also, you try to define unary operator+ when thinking of binary operator+.

bipll
  • 11,747
  • 1
  • 18
  • 32
  • I understand the errors, I think.... I just don't know how to correct them. This is the template I followed for overloading the operators. http://stackoverflow.com/questions/4421706/operator-overloading @bipll – Bigboy6 Apr 18 '16 at 16:06
0

The proximal cause is in these two lines, where you confuse the syntax used for declaring a pointer with the syntax used for dereferencing one:

float *newArr = new float[Capacity];
// ...
*newArr = new float[Capacity];

The declaration float * newArr declares newArr with type float * (pointer-to-float).

Whenever you refer to newArr after this, you're refering to a pointer-to-float, because the variable's type doesn't change.

Given that, the expression *newArr means dereference this pointer, so it's identical to newArr[0].

tl;dr

The simple fix is to remove the leading * from the second line quoted above.


The correct fix is to stop doing all this C-style memory management in the first place, and instead use one of

// smallest change
std::vector<float> coefficients;
std::vector<float> exponents;

// larger change
using ExpCoef = std::pair<float, float>;
std::vector<ExpCoef> terms;

// might actually be better
using Exponent = int;
using Coefficient = float;
std::map<Exponent, Coefficient> terms;

Note in the last case I assumed you only really require integer exponents - you have other issues with comparing float variables if you really need non-integral powers.

Useless
  • 64,155
  • 6
  • 88
  • 132