[RESOLVED] More operator problems, please help!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] More operator problems, please help!

  1. #1
    Join Date
    Dec 2008
    Posts
    12

    [RESOLVED] More operator problems, please help!

    Hi. I've got a problem with this addition operator, that's supposed to concatenate two class objects containing arrays, like so:

    c=a+b

    where: c.data[15] = a.data[7] + b.data[8]

    and: c.size = 15
    a.size = 7
    b.size = 8

    Problem is; my operator adds successfully, but overwrites one of the source classes.
    I'm confused as to how you're supposed to utilise two source objects, and then create a 3rd object to hold the result. Can someone please spell it out for me?

    This is latest revision, but it creates some sort of cyclical error...
    Code:
    	float_seq &operator+(const float_seq &a)													
    	{																							
    		
    		// so create a new object, large enough for both
    		float_seq temp(number_of_elements+a.number_of_elements);
    		
    		for(int i=0;i<=(number_of_elements-1);i++) temp.float_seq::write(i,data[i]);
    		for(int i=0;i<=(a.number_of_elements-1);i++) temp.float_seq::write((i+number_of_elements),a.data[i]);
    
    		number_of_elements = number_of_elements+a.number_of_elements;
    		
    		return (temp);
    	}
    Any help would be greatly appreciated!

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: More operator problems, please help!

    Code:
    temp.number_of_elements = number_of_elements+a.number_of_elements;
    ?
    - petter
    I love deadlines. I like the whooshing sound they make as they pass by - Douglas Adams.
    Visit me!.

    Use code-tags! [code]Your code here[/code]

  3. #3
    Join Date
    Dec 2008
    Posts
    12

    Re: More operator problems, please help!

    Hi petter - Thanks for the reply. I had missed that, but that's not the issue anyway. I've cleaned it up a bit, could you have another look?

    Code:
    	float_seq &operator+(const float_seq &a)													
    	{																							
    		
    		// so create a new object, large enough for both
    		float_seq *temp = new float_seq(number_of_elements+a.number_of_elements);
    		
    		for(int i=0;i<=(number_of_elements-1);i++) temp->data[i] = data[i];
    		for(int i=0;i<=(a.number_of_elements-1);i++) temp->data[i] = a.data[i+number_of_elements];
    
    		temp->number_of_elements = number_of_elements+a.number_of_elements;
    		
    		return (*temp);
    	}
    Last edited by dan56965; December 2nd, 2008 at 06:39 AM. Reason: revised code - now compiles, but result given is wrong

  4. #4
    Join Date
    May 2002
    Posts
    1,435

    Re: More operator problems, please help!

    operator + should return a float_seq object - not return a reference to a temporary object as you do in both versions of your code.

    The temporary object should not be allocated by new inside the operator function because it will never be deleted. The original version correctly allocates float_seq as a local-scoped variable. Note that a+b implies that a temporary float_seq is created and THEN the operator = (or possibly copy constructor) is applied when assigning to c.

    Make sure that you have implemented both the copy constructor and assignment operator (=). For POD classes they are not always necessary, but since your class contains arrays I believe that they are.

    These two lines seem to be a big source of trouble:
    Code:
    for(int i=0;i<=(number_of_elements-1);i++) temp->data[i] = data[i];
    for(int i=0;i<=(a.number_of_elements-1);i++) temp->data[i] = a.data[i+number_of_elements];
    I think this is what you want:
    Code:
    for(int i=0;i<=(number_of_elements-1);i++) temp->data[i] = data[i];
    for(int i=0;i<=(a.number_of_elements-1);i++) temp->data[i+number of elements] = a.data[i];
    A minor point: you should always use const when a function does not modify the class.

    float_seq operator +(const float_seq &a) const
    Last edited by 0xC0000005; December 2nd, 2008 at 07:49 AM.

  5. #5
    Join Date
    Dec 2008
    Posts
    12

    Re: More operator problems, please help!

    Hi again. Thanks for the reply 0xC0000005.

    I'm afraid it still isn't working - when I call the operator the program hangs. When I try to 'cout << X+Y;' for example, there doesn't seem to be a valid value in the number_of_elements field, as it the thing it's trying to print is no longer there.
    Can anyone shed some light?

    Thanks again for any help.


    This is the revised code for the operator:
    Code:
    	float_seq &operator+(const float_seq &a) const													
    	{																							
    
    		float_seq temp(number_of_elements+a.number_of_elements);
    		
    		for(int i=0;i<=(number_of_elements-1);i++) temp.data[i] = data[i];
    		for(int i=0;i<=(a.number_of_elements-1);i++) temp.data[i+number_of_elements] = a.data[i];
    
    		temp.number_of_elements = number_of_elements+a.number_of_elements;
    		
    		return (temp);
    	}
    Last edited by dan56965; December 2nd, 2008 at 01:58 PM. Reason: screwed up the post....

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    Re: More operator problems, please help!

    You are still returning a reference here:

    float_seq &operator+(const float_seq &a) const


    I tried your code with reference removed and it worked:

    float_seq operator+(const float_seq &a) const

  7. #7
    Join Date
    Dec 2008
    Posts
    12

    Re: More operator problems, please help!

    Ah.... Success!

    Although, I'm surprised the compiler doesn't catch something like that out, but I guess there's a reason for that....

    Anyway, thanks again for your time, it is greatly appreciated.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center