I have a problem with my program in c++. I try to write copy constructor. Everything is good until I start copying elements from array from object that is applied as a reference. I can print every element of applied array but I can't copy it to array in object that copy constructor will create.
Here's the code (I show only the header file and some methods that needs copy constuctor because all code is too long I think)
Header.h
#pragma once
# include <iostream>
# include <fstream>
# include <cmath>
using namespace std;
class Polynomial {
private:
int degree;
double* coefficients;
public:
static const string errors[5];
Polynomial(const Polynomial& wzor); // konstruktor kopiujacy
Polynomial(float n);
~Polynomial();
void setCoefficient(unsigned int i, double value);
double getCoefficient(unsigned int i);
double value(double x);
friend ostream & operator <<(ostream & out, Polynomial & p);
double operator[](const int index);
double operator()(double x);
void operator=(Polynomial &obiekt);
friend Polynomial operator+(Polynomial &obiekt,Polynomial &obiekt1);
friend Polynomial operator-(Polynomial &obiekt,Polynomial &obiekt1);
friend Polynomial operator~(Polynomial &obiekt);
friend Polynomial operator*(Polynomial &obiekt,Polynomial &obiekt1);
friend Polynomial operator*(Polynomial &obiekt,double x);
};
Polynomial::Polynomial(float n) {
if(n<0){
throw 0;
}
else if(floor(n)-n != 0){
throw 2;
}
degree=(int)n;
coefficients=new double [(int)n+1];
for(unsigned int i=0; i<n; i++) {
coefficients[i]=0.0;
}
coefficients[(int)n]=1.0;
}
Polynomial::Polynomial(const Polynomial &wzor){
degree=wzor.degree; // it's allright
for(int i=0; i<=wzor.degree; i++){
coefficients[i]=wzor.coefficients[i]; // compilator says that this line is wrong
}
}
Polynomial::~Polynomial(){
delete coefficients;
}
Polynomial operator+(Polynomial &obiekt,Polynomial &obiekt1){
Polynomial nowy(1);
if(obiekt1.degree > obiekt.degree){
nowy=obiekt1;
for(int i=0; i<=obiekt.degree; i++){
nowy.coefficients[i]=nowy.coefficients[i]+obiekt.coefficients[i];
}
}
else{
nowy=obiekt;
for(int i=0; i<=obiekt1.degree; i++){
nowy.coefficients[i]=nowy.coefficients[i]+obiekt1.coefficients[i];
}
}
return nowy;
}