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
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){
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
constdouble &operator[] (int k) const;
double &operator[] (int k);
const Polynomial operator+(const Polynomial &, const Polynomial &); //poly+\
poly
const Polynomial operator+(const Polynomial &, constdouble); //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 = newdouble[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 = newdouble[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 = newdouble[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) constconst
{
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() constconst
{
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() constconst
{
return n;
}
constdouble &
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, constdouble 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.