Overloading the += operator isn't working

My += operator's acting as if it's...not doing anything. My friend and I can't figure out what the problem is...so I'm resorting to you guys, the CPlusPlus community, again.

stringArray.cpp
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
// Constructor
stringArray::stringArray (const size_t max_size)
{
	// Sets the number of items/data in the bag to 0
	count = 0;
	// Sets the bag's capacity size to whatever the parameter is
	capacity = max_size;
	// Dynamically creates space for strings in the bag
	data = new stringArray::str[max_size];
}


stringArray& stringArray::operator += (const stringArray& bag1)
{
    // Temporary variable for the if statement
    size_t tempcount;
    tempcount = count + bag1.count;

    // Temporary variable to return
    stringArray tempbag;
    tempbag.count = count + bag1.count;

    // Temporary variable for the for-loop
    size_t tempbag1 = 0;

    // If the count in both do NOT exceed the capacity of the main/1st bag, combine the two
    // Does nothing if it exceeds
    if (tempcount <= capacity)
    {
        // Initializes the first data into the bag
        for (int j = 0; j < count; j++)
        {
            tempbag.data[j] = data[j];
        }

        // Use a for loop to add the strings into the bag
        for (int i = 0; i < tempbag.count; i++)
        {
           // Adds the string from bag1 into tempbag
            tempbag.data[count - 1 + i] = bag1.data[tempbag1];
            tempbag1++;
        }

        return tempbag;
    }
}


stringArray.h
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
#ifndef STRINGARRAY_H_INCLUDED
#define STRINGARRAY_H_INCLUDED

class stringArray
{
    public:
        // Constructor and a copy constructor
        stringArray (const size_t max_size = DEFAULT_SIZE);
        stringArray (const stringArray& source);
        // Destructor
        ~stringArray();

        // Adds a new string in the bag
        // Returns true if successful; false otherwise
        bool addString(const char new_string[ ]);

        // Removes one occurrence of old_string from the bag
        // Returns true if successful; false otherwise
        bool removeString(const char old_string[ ]);

        // Removes all occurrences of old_string from the bag
        // Returns number of strings removed
        // May need to be changed to an int
        size_t removeAll(const char old_string[ ]);

        // Substitutes/replaces one occurrence of old_string with new_string
        // Returns true if successful; false otherwise
        bool substitute(const char old_string[ ], const char new_string[ ]);

        // Substitutes/replaces all occurrences of old_string with new_string
        // Returns number of strings replaced
        size_t substituteAll(const char old_string[ ], const char new_string[ ]);

        // Overloading the += operator
        // Creates a union of two stringArrays/containers
        stringArray& operator += (const stringArray& bag1);

        // Overloading the == operator
        // Friend function to access the private member variables within the bag
        friend bool operator == (const stringArray &bag1, const stringArray& bag2);

        // Overloading the != operator
        // Friend function to access the private member variables within the bag
        friend bool operator != (const stringArray &bag1, const stringArray& bag2);

        // Returns total number of strings in the bag
        size_t size( ) const;

        // Returns true of cur_string is in the bag; false if not
        bool is_string(const char cur_string[ ]) const;

        // Returns number of occurrences of cur_string
        size_t occurrences(const char cur_string[ ]) const;

        // Prints out all strings in the object
        // One string per line
        void printStringArray( ) const;

        // Prints all strings out in the object
        // One string per line
        // Sorted order from least to greatest
        void printSortedStringArray( ) const;

        // Sets the default_size/capacity of the bag to 25
        static const size_t DEFAULT_SIZE = 25;

        // Typedef stuff
        typedef const char* str;

    private:
        // Member variables here
        // count = The number of items in the bag
        size_t count;
        // capacity = The number of items that can be stored in the bag
        size_t capacity;
        // data = The item in the bag
        str* data;
};

#endif // STRINGARRAY_H_INCLUDED 


main.cpp
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
#include <iostream>
#include <cstdlib>
#include "stringArray.h"

using namespace std;
//using namespace csci2270_assignmentTwo;

	int main( )
	{
	    stringArray sa1, sa2(100);
	    sa1.addString("a");
	    sa1.addString("b");
	    sa1.addString("c");
	    sa1.printStringArray();
	    cout << "\n";

	    sa2.addString("1");
	    sa2.addString("2");
	    sa2.addString("3");
	    sa2.printStringArray();
	    cout << "\n";

	    sa1 += sa2;
	    sa1.printStringArray();
	    cout << "---------------------" << "\n";
	    sa2.printStringArray();
            return EXIT_SUCCESS;
}


This program should be printing out:
a
b
c

1
2
3

Tempbag's count: 0
Count: 3
a
b
c
1
2
3
---------------------
1
2
3


But instead, it's printing out:

a
b
c

1
2
3

Tempbag's count: 0
Count: 3
a
b
c
---------------------
1
2
3
You are returning a reference to a local variable. See lines 13, 20, and 44 of stringArray.cpp. Usually, the operator+= appends data to the lvalue *this rather than creating an entirely new object.
Last edited on
Oh, that explains why...what's the workaround it though?

EDIT: I feel DUMB. I didn't know *this was a key word. Thanks.
Last edited on
You could try assigning the value of tempbag to *this and returning *this. Just be sure to delete any previously allocated memory before replacing 'data'.

Hope this helps.
Last edited on
try this .

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
stringArray& stringArray::operator += (const stringArray& bag1)
{
    // Temporary variable for the if statement
    size_t tempcount;
    tempcount = count + bag1.count;

    // Temporary variable to return
    stringArray tempbag;
    tempbag.count = count + bag1.count;

    // Temporary variable for the for-loop
    size_t tempbag1 = 0;

    // If the count in both do NOT exceed the capacity of the main/1st bag, combine the two
    // Does nothing if it exceeds
    if (tempcount <= capacity)
    {
        // Initializes the first data into the bag
        for (int j = 0; j < count; j++)
        {
            tempbag.data[j] = data[j];
        }

        // Use a for loop to add the strings into the bag
        int tempcout = 0;
        for (int i = count; i < (tempbag.count + count); i++)
        {
           // Adds the string from bag1 into tempbag
            tempbag.data[ i] = bag1.data[tempcount++];

        }

        return tempbag;
  }
else
  {
        return bag1;
   }
}

   
I got it; it just took me a while to figure out what wasn't needed and to replace what with what. Thank you both!
I agree with moorecm . thanks moorecm .
No no no no no.

operator+= must modify its left-hand side.

What would you expect the output of this code to be:

1
2
3
int x = 4;
std::cout << (x += 2) << std::endl;
std::cout << x << std::endl;


Hopefully you expect it to output 6 in both cases. If it worked the way your
operator+= works, it would output 6 and 4.

operator+= must modify *this and return *this to be parallel to the way
operator+= works for any other type.

Topic archived. No new replies allowed.