CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member Join Date
Dec 2008
Posts
12

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 = a.data + b.data

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! Reply With Quote

Code:
`temp.number_of_elements = number_of_elements+a.number_of_elements;`
?
- petter Reply With Quote

3. Junior Member Join Date
Dec 2008
Posts
12

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 07:39 AM. Reason: revised code - now compiles, but result given is wrong Reply With Quote

4. Senior Member Join Date
May 2002
Posts
1,435

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 08:49 AM. Reply With Quote

5. Junior Member Join Date
Dec 2008
Posts
12

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 02:58 PM. Reason: screwed up the post.... Reply With Quote

6. Senior Member Join Date
May 2002
Posts
1,435

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 Reply With Quote

7. Junior Member Join Date
Dec 2008
Posts
12

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. Reply With Quote

c++, class, concatenate, operator #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
{"ft_lang_label":"__label__en","ft_lang_prob":0.82417005,"math_prob":0.93067867,"size":5026,"snap":"2020-24-2020-29","text_gpt3_token_len":1348,"char_repetition_ratio":0.19633612,"word_repetition_ratio":0.10518934,"special_character_ratio":0.2908874,"punctuation_ratio":0.19708738,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96491265,"pos_list":[0,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,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-14T19:43:13Z\",\"WARC-Record-ID\":\"<urn:uuid:24bd7733-d351-45c3-b60c-c552eaae9c16>\",\"Content-Length\":\"124696\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e653e385-00d5-47e1-b5ed-299506f48732>\",\"WARC-Concurrent-To\":\"<urn:uuid:86feee65-3771-46e1-b94b-31897e30cd65>\",\"WARC-IP-Address\":\"3.20.36.137\",\"WARC-Target-URI\":\"http://forums.codeguru.com/showthread.php?466397-RESOLVED-More-operator-problems-please-help!&s=cde9415e46d21793feb032e8b40b164f\",\"WARC-Payload-Digest\":\"sha1:KIR3GKZYLIH3CRVVVWUJAJQ4KKHQVDID\",\"WARC-Block-Digest\":\"sha1:AEUB3XDHOML3GWRXUMKJK75ITSWLJVMQ\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593657151197.83_warc_CC-MAIN-20200714181325-20200714211325-00255.warc.gz\"}"}