Non member function error - Overloading Polynomials

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

This is the error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    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];
   ^

Here is the code
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
#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


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
#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;   
}
closed account (48bpfSEw)
You have to differenciate:

float Value; // the compiler reserves a place for the Value of the size of float (4 Bytes)

float* ptrValue; // the compiler reserves a place for ptrValue of the size of a pointer (4 Bytes in a 32 Bit System). ptrValue has to be initialized else it points anywhere randomly.

ptrValue = &Value; // ptrValue points now to the address of "Value"

Value = 1.0; // puts the value 1.0 to the place of the address of Value
*ptrValue = 2.0; // puts the value 2.0 to the place where ptrValue points to. In this case it overwrites the Value




So where does ptrValue = &Value; go in the program?

Thanks
closed account (48bpfSEw)
this is the buggy spot:

//resize exponent array
*newArr = new float[Capacity];


"*newArr" is from type "float" and not from type "float*" (pointer)

"new float[]" ist from type "float*"


so, correct code:

newArr = new float [Capacity];




So where does ptrValue = &Value; go in the program?

Anywhere you want to store the address of an object called Value. You don't actually have anything called Value in your coce. Necip was giving you examples of how to use pointers.
Last edited on
Topic archived. No new replies allowed.