Overloading Operator - argument error

hey all!
i've written a class Polynomial with several methods

i'd like to add 2 polynomials and add a scalar to a polynomial, therefore i wrote 2 methods but my compiler keeps giving me an error i dont understand.

i hope you can help me

**so here's my header-file**



class Polynomial{
private:
int n; //degree
double* a; //vector of coefficients

public:
Polynomial(int); //constructor
~Polynomial(); //destructor
Polynomial(const Polynomial&); //copy constructor

Polynomial& operator=(const Polynomial&); //assignment operator

double getCoefficient(int)const; //returns j-th coefficient
void setCoefficient(int j, double aj);
void printPoly()const;

int size() const; //returns degree

const double& operator[](int k) const;
double& operator[](int k);

const Polynomial operator+(const Polynomial&, const Polynomial&); //poly+poly
const Polynomial operator+(const Polynomial&, const double); //poly+scalar

};


**and now the source-code**

#include "polynomial.hpp"
#include <iostream>
#include <cassert>

using std::cout;
using std::cin;
using std::endl;

Polynomial::Polynomial(int n){
assert(n>=0);
this->n = n;

a = new double[n+1];

for(int j=0; j<=n; ++j){
a[j] = 0;
}

cout << "Polynomial is constructed" << endl;
}

Polynomial::~Polynomial(){
if(n >= 0){
delete[] a;
}
cout << "Polynomial of length " << n << " got deleted" << endl;
}

Polynomial::Polynomial(const Polynomial& rhs){
n = rhs.size();

a = new double[n+1];
for(int j=0; j<=n; ++j){ //vl <n oder j++?
a[j] = rhs[j];
}

cout << "Polynomial of length "<< n <<" got copied" << endl;
}

Polynomial& Polynomial::operator=(const Polynomial& rhs){
if(n == rhs.size()){
for(int j=0; j<=n; ++j){
a[j] = rhs[j];
}
}
else {
if (n > 0) {
delete[] a;
}
n = rhs.size();
if (n == 0) {
a = (double*) NULL;
}
else {
a = new double[n+1];
for (int j=0; j<=n; ++j) {
a[j] = rhs[j];
}
}
}

cout << "Polynomial of length "<< n << " got assigned" << endl;
}

double Polynomial::getCoefficient(int j)const{
assert(j>= 0 && j<n+1);
return a[j];
}

void Polynomial::setCoefficient(int j, double aj){
assert(j >= 0 && j <= n);
a[j] = aj;
}

void Polynomial::printPoly()const{

int j = 0;
double aj;

for(j=0; j<=n; j++){

aj = getCoefficient(j);
cout << aj << "*x^" << j;

if(j<n){
cout << " + ";
}
}

cout << endl << endl;
}

int Polynomial::size() const{
return n;
}

const double& Polynomial::operator[](int k) const{
assert(k>=0 && k<=n);
return a[k];
}

double& Polynomial::operator[](int k){
assert(k>=0 && k<=n);
return a[k];
}

//sum of 2 polynomials
const Polynomial operator+(const Polynomial& p, const Polynomial& q){
int j = 0;
double c = 0; //coefficients



if(p.size() <= q.size()){

int m = p.size();

Polynomial sum(m);

for(j = 0; j<=getPolyDegree(sum); j++){

a = getPolyCoefficient(p, j) + getPolyCoefficient(q,j);
setPolyCoefficient(sum, j, a);
}
return sum;
}
else{
sum = newPoly(getPolyDegree(p)); //allokiere Speicher fuer hoeheren Grad
for(j = 0; j<=getPolyDegree(sum); j++){

a = getPolyCoefficient(p, j) + getPolyCoefficient(q,j);
setPolyCoefficient(sum, j, a);
}
return sum;
}


}


//polynomial + scalar
const Polynomial operator+(const Polynomial& p, const double b){

double a0_new = 0;

a0_new = p.getCoefficient(0) + b;

p.setCoefficient(0, a0_new);
}





when i compile this with g++ i get

In file included from polynomial.cpp:1:0:
polynomial.hpp:28:61: error: ‘const Polynomial Polynomial::operator+(const Polynomial&, double)’ must take either zero or one argument
const Polynomial operator+(const Polynomial&, const double);
^

polynomial.cpp: In function ‘const Polynomial operator+(const Polynomial&, double)’:
polynomial.cpp:124:28: error: passing ‘const Polynomial’ as ‘this’ argument discards qualifiers [-fpermissive]
p.setCoefficient(0, a0_new);
^

polynomial.cpp:70:6: note: in call to ‘void Polynomial::setCoefficient(int, double)’
void Polynomial::setCoefficient(int j, double aj){


--------------------------------------------------------------------------------








i've naively tried fixing it with attaching 0 to the input parameter's methods (didnt work)

i'd be utterly grateful if somebody could help me!

by the way, im a newbie so it'd be awesome if you could be patient with me

thank you!
Last edited on
If you declare operator+() to be a member function then it should take just one argument. It adds the argument to *this.
1
2
const Polynomial operator+(const Polynomial&); //poly+poly
const Polynomial operator+(const double); //poly+scalar 


As gunnerfunner pointed out, please use code tags. WHile composing a post, highlight the code and click the <> button to the right of the edit window.

Here is your code indented and with tags. Comments below.

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
class Polynomial
{
  private:
    int n;                      //degree
    double *a;                  //vector of coefficients

  public:
      Polynomial(int);          //constructor
     ~Polynomial();             //destructor
      Polynomial(const Polynomial &);   //copy constructor

      Polynomial & operator=(const Polynomial &);       //assignment operator

    double getCoefficient(int) const;   //returns j-th coefficient
    void setCoefficient(int j, double aj);
    void printPoly() const;

    int size() const;           //returns degree

    const double &operator[] (int k) const;
    double &operator[] (int k);

    const Polynomial operator+(const Polynomial &, const Polynomial &); //poly+\
poly
    const Polynomial operator+(const Polynomial &, const double);       //poly+\
scalar

};


#include <iostream>
#include <cassert>
using std::cout;
using std::cin;
using std::endl;

Polynomial::Polynomial(int n)
{
    assert(n >= 0);
    this->n = n;

    a = new double[n + 1];

    for (int j = 0; j <= n; ++j) {
        a[j] = 0;
    }

    cout << "Polynomial is constructed" << endl;
}

Polynomial::~Polynomial()
{
    if (n >= 0) {
        delete[]a;
    }
    cout << "Polynomial of length " << n << " got deleted" << endl;
}

Polynomial::Polynomial(const Polynomial & rhs)
{
    n = rhs.size();

    a = new double[n + 1];
    for (int j = 0; j <= n; ++j) {               //vl <n oder j++?
        a[j] = rhs[j];
    }

    cout << "Polynomial of length " << n << " got copied" << endl;
}

Polynomial & Polynomial::operator=(const Polynomial & rhs)
{
    if (n == rhs.size()) {
        for (int j = 0; j <= n; ++j) {
            a[j] = rhs[j];
        }
    } else {
        if (n > 0) {
            delete[]a;
        }
        n = rhs.size();
        if (n == 0) {
            a = (double *) NULL;
        } else {
            a = new double[n + 1];
            for (int j = 0; j <= n; ++j) {
                a[j] = rhs[j];
            }
        }
    }

    cout << "Polynomial of length " << n << " got assigned" << endl;
}

double
Polynomial::getCoefficient(int j) const const
{
    assert(j >= 0 && j < n + 1);
    return a[j];
}

void
Polynomial::setCoefficient(int j, double aj)
{
    assert(j >= 0 && j <= n);
    a[j] = aj;
}

void
Polynomial::printPoly() const const
{

    int j = 0;
    double aj;

    for (j = 0; j <= n; j++) {

        aj = getCoefficient(j);
        cout << aj << "*x^" << j;

        if (j < n) {
            cout << " + ";
        }
    }

    cout << endl << endl;
}

int
Polynomial::size() const const
{
    return n;
}

const double &
Polynomial::operator[] (int k) const
{
    assert(k >= 0 && k <= n);
    return a[k];
}

double &
Polynomial::operator[] (int k)
{
    assert(k >= 0 && k <= n);
    return a[k];
}

//sum of 2 polynomials
const Polynomial
operator+(const Polynomial & p, const Polynomial & q)
{
    int j = 0;
    double c = 0;                       //coefficients

    if (p.size() <= q.size()) {
        int m = p.size();
        Polynomial sum(m);

        for (j = 0; j <= getPolyDegree(sum); j++) {
            a = getPolyCoefficient(p, j) + getPolyCoefficient(q, j);
            setPolyCoefficient(sum, j, a);
        }
        return sum;
    } else {
        sum = newPoly(getPolyDegree(p));         //allokiere Speicher fuer hoeh\
eren Grad
        for (j = 0; j <= getPolyDegree(sum); j++) {
            a = getPolyCoefficient(p, j) + getPolyCoefficient(q, j);
            setPolyCoefficient(sum, j, a);
        }
        return sum;
    }

}

//polynomial + scalar
const Polynomial
operator+(const Polynomial & p, const double b)
{

    double a0_new = 0;

    a0_new = p.getCoefficient(0) + b;
    p.setCoefficient(0, a0_new);
}


Line 39. Since n should never be negative, why not change Polynomial make it an unsigned?

Lines 53 & 54 vs lines 78 & 79: These do different things if n==0. They should do the same thing.

Line 110: it would be more flexible if printpoly() printed to a stream that you pass as an argument. That way you could print it to any stream at all. Better yet, change this to be operator<< so it's consistent with the base types.
thank you very much, its now perfectly working!
Topic archived. No new replies allowed.