Salve, sto svolgendo un esercizio sui polinomi, volevo sapere perchè nel file header se scrivo Poly& Poly::operator=( const Poly& q ); mi dà come errore ' extra qualification for Poly:: on member operator = [f-permissive]' . Ho scritto ciò perchè nel file di implementazione attraverso questa dichiarazione ho poi scritto questo.
Poly& Poly::operator=( const Poly& q )
{
if (this != &q)
{
delete [] coef;
deg = q.getDeg();
double *qcoef = q.getCoef();
coef = new double[deg+1];
for (int i=0; i<=deg; i++)
coef[i] = qcoef[i];
}
Qui l'intero codice:
poly.h
#include <iostream>
using namespace std;
class Poly {
private:
int deg;
double *coef;
public:
Poly(void);
Poly(int n, double *a);
Poly( const Poly& q );
Poly& Poly::operator=( const Poly& q );
~Poly(void);
int getDeg(void) const;
double *getCoef(void) const;
Poly& scale( double k );
Poly& shiftby( const Poly& q );
double eval( double x )const;
friend Poly operator+( const Poly& p, const Poly& q );
friend Poly operator*( double k, const Poly& p );
void print( char *nome ) const;
};
poly.cpp
* poly.cpp
*
* Created on: 06/mag/2014
* Author: raffaele
*/
#include <iostream>
#include "poly.h"
using namespace std;
Poly::Poly( void )
{
coef = new double[1];
deg = 0;
coef[0] = 0;
}
Poly::Poly( int n, double *a )
{
deg = n;
coef = new double[n+1];
for (int i=0; i<=n; i++)
coef[i] = a[i];
}
Poly::Poly( const Poly& q ) // copy constructor
{
deg=q.getDeg();
double *qcoef=q.getCoef();
coef = new double[deg+1];
for (int i=0; i<=deg; i++)
coef[i] = qcoef[i];
}
Poly& Poly::operator=( const Poly& q )
{
if (this != &q)
{
delete [] coef;
deg = q.getDeg();
double *qcoef = q.getCoef();
coef = new double[deg+1];
for (int i=0; i<=deg; i++)
coef[i] = qcoef[i];
}
return *this;
}
int Poly::getDeg(void) const
{
return deg;
}
double *Poly::getCoef(void) const
{
return coef;
}
void Poly::print( char *nome ) const
{
if (nome != NULL)
cout << "Polinomio " << nome;
cout << ": grado = " << deg << "; ";
for (int i=0; i<=deg; i++)
cout << "[" << i <<"]=" << coef[i] << "; ";
cout << '\n';
}
Poly::~Poly( void )
{
delete [] coef;
}
Poly& Poly::scale( double k )
{
for (int i = 0; i <= deg; i++)
coef[i] *= k;
return *this;
}
Poly& Poly::shiftby( const Poly& q )
{
int i;
if (q.deg > deg)
{
double *newvec = new double[q.deg+1];
for (i=0; i<=q.deg; i++)
newvec[i] = (i > deg)? 0: coef[i];
delete [] coef;
coef = newvec;
deg = q.deg;
}
for (i = 0; i <= q.deg; i++)
coef[i] += q.coef[i];
return *this;
}
double Poly::eval( double x ) const
{
double val = 0;
for (int i = deg; i >= 0; i--)
val = val*x+coef[i];
return val;
}
Poly operator+( const Poly& p, const Poly& q )
{
Poly temp(p);
temp.shiftby( q );
return temp;
}
Poly operator*( double k, const Poly& p )
{
Poly temp(p);
temp.scale( k );
return temp;
}
mainpoly.cpp
#include <iostream>
#include "poly.h"
using namespace std;
int main()
{
int i, n;
double *temp, k;
// leggiamo due polinomi
cout << "assegna grado e coefficienti a_0, a_1, .., a_n di p\n ";
cin >> n;
temp = new double[n+1];
for (i=0; i<=n; i++)
cin >> temp[i];
Poly p(n, temp);
delete [] temp;
p.print("p");
cout << "p(1) = " << p.eval(1) << "\n";
cout << "assegna grado e coefficienti b_0, b_1, .., b_n di q \n";
cin >> n;
temp = new double[n+1];
for (i=0; i<=n; i++)
cin >> temp[i];
Poly q(n, temp);
delete [] temp;
q.print("q");
cout << "q(1) = " << q.eval(1) << "\n";
cout << "\nInizializzo r col valore 7*p+q e lo stampo\n";
Poly r(7*p+q);
r.print("r");
cout << "\nDichiaro r1, gli assegno il valore p+q+r e lo stampo\n";
Poly r1;
r1 = p+q+r;
r1.print("r1");
cout << "\nStampo il polinomio 100*q\n";
(100*q).print("100*q");
cout << "\nMoltiplico q per 4 e lo stampo\n";
q.scale(4.); // scalo q di 4;
q.print("q"); // e lo stampo
cout << "\nSommo a p il polinomio q e stampo p\n";
p.shiftby(q); // sommo q in p e stampo p;
p.print("p");
return 0;
}