Próbuję napisać sobie program następujący:
wiel_prog.cpp
[code]
#include <iostream>
#include "wiel.h"
int main()
{
//1 p.
cout<<endl<<"------ ETAP 1 ------"<<endl;
double tab1[]={1,2,3,4}; // w(x)=4*x<sup>3+3*x</sup>2+2*x<sup>1+1*x</sup>0
Wiel w1(3,tab1); // stopieñ wielomianu
// oraz tablica wspó³czynników
cout<<"w1(x)="<<w1<<endl;
//0.5 p.
cout<<endl<<"------ ETAP 2 ------"<<endl;
Wiel w2(w1),w3;
cout<<"w2(x)="<<w2<<endl;
cout<<"w3(x)="<<w3<<endl;
//0.5 p.
cout<<endl<<"------ ETAP 3 ------"<<endl;
w3=w2;
cout<<"w3(x)="<<w3<<endl;
w2[3]=2;
cout<<"w2(x)="<<w2<<endl;
//1 p.
cout<<endl<<"------ ETAP 4 ------"<<endl;
double x=2;
cout<<"w1(x)="<<w1<<endl;
cout<<"w1(2)="<<w1(x)<<endl; // wartoϾ wielomianu
// dla argumentu x=2
// zastosuj algorytm Hornera
//1 p.
cout<<endl<<"------ ETAP 5 ------"<<endl;
w3=w1.pochodna();
cout<<"w1(x) = "<<w1<<endl;
cout<<"w1'(x) = "<<w3<<endl;
cout<<"w1'(2)="<<w3(x)<<endl;
Wiel w4 = w3.pochodna();
cout<<"w1''(x) = "<<w4<<endl;
w4 = w4.pochodna();
cout<<"w1`(x) = "<<w4<<endl;
w4 = w4.pochodna();
cout<<"w1`'(x) = "<<w4<<endl;
w4 = w4.pochodna();
cout<<"w1`''(x)= "<<w4<<endl;
//0.5 p.
/*
cout<<endl<<"------ ETAP 6 ------"<<endl;
cout<<"w1(x)="<<w1<<endl;
cout<<"w3(x)="<<w3<<endl;
w4=w3+w1; cout<<"w4(x)=w3(x)+w1(x)="<<w4<<endl;
/
//0.5 p.
/
cout<<endl<<"------ ETAP 7 ------"<<endl;
//WSKAZOWKA: zdefiniuj i wykorzystaj metode prywatna:
// Wiel& operator-();
w4=w1-w3; cout<<"w4(x)=w1(x)-w3(x)="<<w4<<endl;
w4=w3-w1; cout<<"w4(x)=w3(x)-w1(x)="<<w4<<endl;
w4=w3-w3; cout<<"w4(x)=w3(x)-w1(x)="<<w4<<endl;
*/
return 0;
}
[/code]
wiel.cpp
[code]
#include "wiel.h"
using namespace std;
void log (char *);
void log (char *string)
{
cerr << string << endl;
}
Wiel::Wiel (Wiel &c)
{
this->n = c.n;
this->a = new double[n+1];
for (int i = 0; i<=c.n; i++)
this->a[i] = c.a[i];
}
Wiel::Wiel (int n, double *tab)
{
if (n<=0)
{
throw(-1);
log ("Nie mogę utworzyć mniejszej niż 0");
}
this->n = n;
cerr << "tworzę wielomian stopnia " << n;
this->a = new double[this->n+1];
for (int i = 0; i<=this->n; i++) this->a[i] = tab[i];
cerr << "Tworzę wielomian z tablica " << this->a << endl;
}
Wiel::Wiel (int n)
{
if (n<=0) throw (-1);
this->n = n;
cerr << "tworzę wielomian stopnia " << n;
this->a = new double[this->n+1];
for (int i = 0; i<=this->n; i++)
this->a[i] = 0;
cerr << "Tworzę wielomian z tablica " << this->a << endl;
}
Wiel::Wiel ()
{
this->n = 0;
cerr << "tworzę wielomian stopnia " << n;
this->a = NULL;
cerr << "Tworzę wielomian z tablica " << this->a << endl;
}
Wiel::~Wiel()
{
if (this->a != NULL) delete []a;
}
ostream& operator << ( ostream& c, const Wiel& w)
{
if (w.n > 0)
{
for (int i = 0; i<w.n; i++)
cout << w.a[i] << "*x^" << w.n-i << " + ";
cout << w.a[w.n] << "*x^0" << endl;
} else cout << " 0 ";
return c;
}
double& Wiel::operator[] (int i)
{
// błąd
if (i<=this->n) return this->a[i]; else throw(-1);
}
Wiel& Wiel::operator= (Wiel& rhs)
{
//błąd
this->n = rhs.n;
if (this->a != NULL) delete[] this->a;
this->a = new double[rhs.n+1];
for (int i = 0; i<=rhs.n; i++)
this->a[i] = rhs.a[i];
return *this;
}
double& Wiel::operator() (int n)
{
double var = this->a[this->n];
for (int i = n-1; i>=0; i--)
var += var*n + this->a[i];
return var;
}
Wiel Wiel::pochodna ()
{
// błąd
Wiel nowy(this->n-1);
cout << "Stary: " << this << endl;
for (int i = 0; i<=nowy.n; i++)
nowy.a[i] = this->a[i](n-i);
return nowy;
}
/* Wiel Wiel::operator+ (Wiel& rhs)
{
int m;
Wiel nowy (max(this->n, rhs.n), rhs.a);
for (int i = nowy.n; i>=min(this->n, rhs.n); i--)
{
nowy->a[i] += rhs.i;
}
for (i = 0; i<=nowy.n; i++)
if (nowy.a[i] !=0 )
{
m = i;
break;
}
Wiel nowy2(i, nowy.a);
return nowy2;
}
Wiel Wiel::operator- (Wiel& rhs)
{
int m;
Wiel nowy (max(this->n, rhs.n), rhs.a);
for (int i = 0; i<nowy.n; i++)
nowy[i] = rhs[i];
for (int i = n; i>=0; i--)
if (nowy[i]!=0)
{
m = i;
break;
}
Wiel nowy2 (m, nowy.a);
return nowy2;
}
*/
[/code]
wiel.h
[code]
#include <iostream>
using namespace std;
class Wiel
{
int n; // stopień wielomianu
double* a; // tablica współczynników wielomianu
// w(x)=a[n]*xn+a[n-1]*x(n-1)+...+a[1]*x1+a[0]*x0
public:
//.............................................
//.............................................
Wiel (int, double *);
Wiel (Wiel&);
Wiel (int n);
Wiel ();
~Wiel ();
friend ostream& operator << ( ostream&, const Wiel& );
double& operator[] (int n);
Wiel& operator= (Wiel&);
double& operator() (int n);
Wiel& pochodna ();
Wiel operator+ (Wiel& rhs);
Wiel operator- (Wiel& rhs);
};
[/code]
Przy kompilacji wyrzuca to:
[quote]
== xxxtemp, Debug ===
/home/tomasz/Dokumenty/prace/c/xxxtemp/wiel.cpp||In constructor ‘Wiel::Wiel(int, double*)’:|
/home/tomasz/Dokumenty/prace/c/xxxtemp/wiel.cpp|25|warning: deprecated conversion from string constant to ‘char*’|
/home/tomasz/Dokumenty/prace/c/xxxtemp/wiel.cpp||In member function ‘double& Wiel::operator()(int)’:|
/home/tomasz/Dokumenty/prace/c/xxxtemp/wiel.cpp|93|warning: reference to local variable ‘var’ returned|
/home/tomasz/Dokumenty/prace/c/xxxtemp/wiel.cpp|99|error: prototype for ‘Wiel Wiel::pochodna()’ does not match any in class ‘Wiel’|
/home/tomasz/Dokumenty/prace/c/xxxtemp/wiel.h|23|error: candidate is: Wiel& Wiel::pochodna()|
== Build finished: 2 errors, 2 warnings ===
[/quote]
Co robię nie tak? Gdy próbowałem zamienić referencję na zwykły obiekt, program kompilował się, ale pojawiał się też run-time error, przy przypisywaniu za pomocą operator= (null pointer w tablicy double)