Thread: Overloaded operator+ chaining having some problems

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    Question 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.

    overloaded operator+:

    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!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    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?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laczfinador View Post
    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.

    ...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.

    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 ];

    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Quote Originally Posted by laczfinador View Post
    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.
    I'll have to guess because you did not provide the code or actual error, but the error probably stems from the fact that your pointers are just pointers and you try to point them to a const object. That will give an error. Make them const pointers (const NagyonNagySzam*) and it should work.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Thank you for your answers so far! I will try your suggestions and give feedback once I'm done - or struck. :P

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Well, my original problem looks to solved. On the other hand, some other has arised, although they are not related to this thread. Thank you once more!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  2. Overloaded << operator
    By DivineSlayer936 in forum C++ Programming
    Replies: 19
    Last Post: 04-07-2007, 12:46 PM
  3. Need help with overloaded * operator
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2006, 10:52 AM
  4. Overloaded = operator problems
    By Michty in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2006, 05:57 PM
  5. About Overloaded Operator. Please Help
    By Antigloss in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2005, 07:48 AM

Tags for this Thread