# Overloaded operator+ chaining having some problems

• 11-01-2011
Overloaded operator+ chaining having some problems
Hello world!
I have an overloaded operator+, which behaves a bit strange. It workes flawlessly when chaining two operations at most, but gives a runtime error, when chaining more than two operations. I could not find and answer to why this is happening. Could anyone please explain to me what am I doing wrong? I have a feeling, that my problem is maybe related to const-correctness, but could not find an article, which would have explained why am I getting this error... :(

Some additional information, to make things clearer: this is an assignment, where I must implement 'big numbers' with addition and multiplication. I must represent big numbers as a sequence of digits.

Code:

```NagyonNagySzam operator+(NagyonNagySzam a, NagyonNagySzam b ){         //DEKLARÁCIÓK, INICIALIZÁCIÓ             NagyonNagySzam eredmeny;         NagyonNagySzam* hosszabb;         NagyonNagySzam* rovidebb;         if(a.hossz>b.hossz){                 hosszabb=&a;                 rovidebb=&b;         }else{                 hosszabb=&b;                 rovidebb=&a;         }         eredmeny.hossz=hosszabb->hossz + 1;         eredmeny.szamjegyek=new int[ eredmeny.hossz ];                 //ALGORITMUS         eredmeny.szamjegyek[0]=(rovidebb->szamjegyek[0] + hosszabb->szamjegyek[0])%10;         int atvitel=(rovidebb->szamjegyek[0] + hosszabb->szamjegyek[0])/10;         for(int i=1;i<=hosszabb->hossz;++i){                 if(i<rovidebb->hossz){                         eredmeny.szamjegyek[i]=(rovidebb->szamjegyek[i] + hosszabb->szamjegyek[i] + atvitel)%10;                         atvitel=(rovidebb->szamjegyek[i] + hosszabb->szamjegyek[i] + atvitel)/10;                 }else if(i<hosszabb->hossz){                         eredmeny.szamjegyek[i]=(hosszabb->szamjegyek[i] + atvitel)%10;                         atvitel=(hosszabb->szamjegyek[i] + atvitel)/10;                 }else{                         if(atvitel==0){                                 eredmeny.hossz=eredmeny.hossz - 1;                         }else{                                 eredmeny.szamjegyek[i]=atvitel;                                 atvitel=0;                         }                 }         }         return eredmeny; }```
Some hints to the code above: first, I check which 'big number' is longer, then i assign a pointer to the longer and the shorter ones accordingly. Then, I do an addition just like I would do on paper.

main.cpp with everything allright:

Code:

```#include "nagyonnagy.h" #include <iostream> #include <string> int main(){     NagyonNagySzam a("100");     NagyonNagySzam b("20");     NagyonNagySzam c("3");     NagyonNagySzam x("123");     NagyonNagySzam d=x+x+b;         std::cout<<d<<std::endl;                 return 0; }```

main.cpp with runtime error:

Code:

```#include "nagyonnagy.h" #include <iostream> #include <string> int main(){     NagyonNagySzam a("100");     NagyonNagySzam b("20");     NagyonNagySzam c("3");     NagyonNagySzam x("123");     NagyonNagySzam d=x+x+b+a;         std::cout<<d<<std::endl;                 return 0; }```
Um... I don't know what else should I say. I would be glad if someone could help me, 'cause I have used all my last chances with this assignment. :/ Thanks in advance!
• 11-01-2011
nvoigt
Some generell hints:

I can see a "new" but no delete. Is the delete called in the destructor? If so, check how many times construction and destruction of your objects happens because your operator + gets it's operands passed by value and makes a lot of copies. Try passing "const NagyonNagySzam&" as parameters, passing by reference should get rid of the copies.

Try to name your variables and class names in english, it saves you a lot of time communicating with other developers. To me, your whole algorithm is a bunch of randomly shuffled letters. If the names were in plain english, I could read it and point out possible mistakes :)
• 11-02-2011
Hello!
I could not find a way to edit the original post, so here is the code in english:

Code:

```VeryBigNumber operator+(VeryBigNumber a, VeryBigNumber b ){         //DEKLARÁCIÓK, INICIALIZÁCIÓ             VeryBigNumber result;         VeryBigNumber* longer;         VeryBigNumber* shorter;         if(a.length>b.length){                 longer=&a;                 shorter=&b;         }else{                 longer=&b;                 shorter=&a;         }         result.length=longer->length + 1;         result.digits=new int[ result.length ];                 //ALGORITMUS         result.digits[0]=(shorter->digits[0] + longer->digits[0])%10;         int carry=(shorter->digits[0] + longer->digits[0])/10;         for(int i=1;i<=longer->length;++i){                 if(i<shorter->length){                         result.digits[i]=(shorter->digits[i] + longer->digits[i] + carry)%10;                         carry=(shorter->digits[i] + longer->digits[i] + carry)/10;                 }else if(i<longer->length){                         result.digits[i]=(longer->digits[i] + carry)%10;                         carry=(longer->digits[i] + carry)/10;                 }else{                         if(carry==0){                                 result.length=result.length - 1;                         }else{                                 result.digits[i]=carry;                                 carry=0;                         }                 }         }         return result; }```
Unfortunatelly, if I try giving the parameters as const NagyonNagySzam& it gives a compile time error, because I am using pointers inside the body of the function to the longer and shorter numbers. Is there a way to circumvent this? I know there is something like a a reference that would not change the parameter given, but I do not understand how it works exactly.
I don't know what do you mean by new and delete. I am not using heap allocation. I thought that variables declared as simple variables (ie not on the heap with new) are destroyed automatically when they reach the end of the block in which they were declared. Have I missed something on the lecture? :)
• 11-02-2011
Elysia
Quote:

Unfortunatelly, if I try giving the parameters as const NagyonNagySzam& it gives a compile time error...

That right there tells you that you have done something wrong. operator + should highly unlikely modify any of the input arguments.

Quote:

...because I am using pointers inside the body of the function to the longer and shorter numbers. Is there a way to circumvent this? I know there is something like a a reference that would not change the parameter given...
You are thinking of a const reference here, which is precisely what you were suggested to use.

Quote:

I don't know what do you mean by new and delete. I am not using heap allocation.
But you are:
>>result.digits=new int[ result.length ];

Quote:

I thought that variables declared as simple variables (ie not on the heap with new) are destroyed automatically when they reach the end of the block in which they were declared. Have I missed something on the lecture? :)
No, this part is correct.
• 11-02-2011
nvoigt
Quote: