Przekazywanie zwracanego obiektu do konstuktora kopiującego.
Witam. Mam problem jak w temacie, metoda zwraca mi obiekt typu Number i nie moge go przekazać do konstruktora kopiującego.
Main:
#include <iostream>
#include "Number.h"
using namespace std;
void Put(string snumber, Number &cnumber)
{
for(int i = 0; i<snumber.length(); i++)
{
int inumber = (int)snumber[i]-48;
cnumber.setNumber(i,inumber);
}
}
int main()
{
string sa,sb;
cin >> sa >> sb;
Number a(sa.length()),b(sb.length());
Put(sa,a);
Put(sb,b);
Number c (a.Add(&b));
//c.Assign(a.Add(&b));
}
Klasa Number:
#ifndef NUMBER_H
#define NUMBER_H
class Number
{
public:
Number(int ilength = 0);
Number(Number &);
Number Add(Number*);
Number Sub(Number*);
void setLength(int);
void setPositiv(bool);
void setNumber(int, int& ); //dac referencje
void Assign(Number*);
int getLength();
bool getPositiv();
int getNumber(int);
virtual ~Number();
protected:
private:
int i_length;
int *pi_number; // zmienic na wskaznik
bool b_positiv;
};
#endif // NUMBER_H
Raport Number.cpp:
#include "Number.h"
#include "Constants.h"
#include <iostream>
Number::Number(int ilength)
{
i_length = ilength;
b_positiv = true;
pi_number = new int[i_length];
for(int i = 0; i < i_length; i++)
{
*(pi_number+i) = 0;
}
}
Number::Number(Number &cother)
{
i_length = cother.getLength();
b_positiv = cother.getPositiv();
for(int i = 0; i < i_length; i++)
*(pi_number+i) = cother.getNumber(i);
}
Number Number::Add(Number *cother)
{
Number cresult;
int ialength = this->getLength(),
iblength = cother->getLength(),
iclenght = 0,
iexcess = 0,
isum = 0;
if(ialength <= iblength) iclenght = iblength+1;
else iclenght = ialength+1;
cresult.setLength(iclenght);
for(int i = ialength-1, j = iblength-1, k = iclenght-1; (i>=0 || j>=0) && k >=0; i--,j--,k--)
{
//std::cout << " Suma " << i << " " << isum << "= "<< this->getNumber(i) << "+" << cother->getNumber(j) << std::endl;
if(i<0) isum = iexcess + cother->getNumber(j);
else if(j<0) isum = iexcess + this->getNumber(i);
else isum = iexcess + this->getNumber(i) + cother->getNumber(j);
if(isum >= SYS)
{
iexcess = isum/SYS; //stala
std::cout << "Nadmiar: " << iexcess << std::endl;
isum = isum%SYS;
std::cout << "Suma: " << isum << std::endl;
}
else iexcess = 0;
std::cout << std::endl;
cresult.setNumber(k,isum);
}
for(int i = 0; i < cresult.getLength(); i++)
std::cout << cresult.getNumber(i);
return cresult;
}
Number Number::Sub(Number *cother)
{
Number cresult;
int ialength = this->getLength(),
iblength = cother->getLength();
//przypisanie dlugosci
if((ialength == iblength && this->getNumber(0) < cother->getNumber(0)) || ialength < iblength) //sprawdzenie czy liczba przekazywana jest wieksza i od przekazanaej odjecie lokalnej z przeciwnym znakiem
{
cother->Sub(this);
cresult.setPositiv(false);
}//if((ialength == iblength && this->getNumber(0) < cother->getNumber(0)) || ialength < iblength)
else{
int iclenght = 0,
isub = 0;
iclenght = ialength;//przypisanie d³ugosci
int tab[iclenght];
for(int i = ialength-1, j=iblength-1, k = iclenght-1; (i>=0||j>=0)&&k>=0; i--,j--,k--)
{
// std::cout << "petla " << i;
int a = this->getNumber(i), b = cother->getNumber(j);
if(i<0) { tab[i] = a; std::cout <<a-b << " = " << a << std::endl;}
else if(j<0){ tab[i] = -b; std::cout << a-b << " = " << a << "-" << b << std::endl;}
else {tab[i] = a-b; std::cout << a-b << " = " << a << "-" << b << std::endl;}
//cresult.setNumber(i,isub);
}//else
//ustawianie znaku
int ihelp = cresult.getNumber(0);
std::cout << "Ustawianie znaku " << ihelp << "\n";
if(ihelp<0)
{
int p = -ihelp; // zmienic zmienna p ;
cresult.setPositiv(false);
cresult.setNumber(0,p);
}
else cresult.setPositiv(true);
cresult.setLength(iclenght);
//korekta
for(int i = iclenght-1; i > 0; i--)
{
if(cresult.getNumber(i) < 0)
{
int a = cresult.getNumber(i),b = cresult.getNumber(i-1);
std::cout <<"SYS-a: " << SYS-a << "=" << SYS << a << std::endl;
int x = SYS+a;
int y = b-1;
cresult.setNumber(i,x);
cresult.setNumber(i-1,y);
}
}
//wypisanie
}
for(int i = 0; i < cresult.getLength(); i++){
int x = 0;
if(0<cresult.getNumber(i) || 16>cresult.getNumber(i)) cresult.setNumber(i,x);
}
if(!cresult.getPositiv()) std::cout << "-";
for(int i = 0; i<cresult.getLength(); i++)
std::cout << cresult.getNumber(i);
return cresult;
}
void Number::Assign(Number *cother)
{
i_length = cother->getLength();
b_positiv = cother->getPositiv();
for(int i = 0; i<i_length; i++)
{
*(pi_number+i) = cother->getNumber(i); //zmienic na wskaznik
}
}
void Number::setLength(int ilength)
{
i_length = ilength;
}
void Number::setPositiv(bool bpositiv)
{
b_positiv = bpositiv;
}
void Number::setNumber(int iindex, int &inumber)
{
*(pi_number+iindex) = inumber; //zmienic na wskaznik
}
int Number::getLength()
{
return i_length;
}
int Number::getNumber(int iindex)
{
return *(pi_number+iindex);
}
bool Number::getPositiv()
{
return b_positiv;
}
Number::~Number()
{
delete[] pi_number;
}
Raport Błędu:
https://www.dropbox.com/home?preview=Przechwytywanie.PNG