I have a fatal error!

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?
Please show the code for your operator+(), at least.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;
		else	if(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 :(
in lines 5 and 7 I can not declare s, b as class objects.

What is that supposed to mean?

in line 30 it can not return a valid value

Why?

and it causes a processor fault.

No such thing. What is really happening?


Perhaps you forgot to implement a copy constructor.
Your operator+() doesn't return anything unless the signs are equal.
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.
Last edited on
Topic archived. No new replies allowed.