I wrote a class and a operator+ for that.
in operator+ compiler causes a processor fault when i want to create a class-name object and return it.
somthing like this: Number n = a + b; // a, b are Numbers too.
* : I didn't wrote any operator= for that class.
what's the problem?
number number::operator+(number right)
{
if(sgn == right.sgn) // sgn = sign of Number
{
number* b = 0;
b = ( after >= right.after ? this : &right );
number* s = 0;
s = ( this == b ? &right : this );
int i = b->after - s->after;
shift(s->num, s->size, i, 'r');
number sum(b->after + max(b->before, s->before) + 1);
int k = 0, j = 0;
for(int x = 0; x < s->size; x++)
{
j = b->num[x] - int('0') + s->num[x] - int('0') + k;
sum.num[x] = (j % 10) + int('0');
k = j / 10;
}
sum.after = b->after;
shift(s->num, s->size, i, 'l');
i = sum.size - 1;
while(sum.num[i] == '0')
i--;
sum.before = i - sum.after + 1;
sum.sgn = sgn;
if(sum.before > 0)
sum.exponent = sum.before - 1;
elseif(sum.before <= 0)
sum.exponent = ( sum.after > 0 ? i - sum.after : 0 );
return sum ;
}
}//End function 'operator+'
after and before are integers that count number of digits before and after point.
in lines 5 and 7 I can not declare s, b as class objects.
by 'step over' compilation i understood that if i declare s, b, as class objects, in line 30 it can not return a valid value and it causes a processor fault.
of course it is not completed.
what's the problem? Help me :(
I created a copy constructor and instead of pointers to number, objects of number in lines 5 and 7 but if I enter 12 and 13 , it returns 24 !!!
( In a test.cpp program I entered these numbers )
why algorithm is wrong?
( Algorithm is not completed )
You've not answered the questions about the code you've posted an gone on to discuss replacement code that you've not published. The questions are there to help you.