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.
// 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;
}
}
#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(constchar new_string[ ]);
// Removes one occurrence of old_string from the bag
// Returns true if successful; false otherwise
bool removeString(constchar 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(constchar old_string[ ]);
// Substitutes/replaces one occurrence of old_string with new_string
// Returns true if successful; false otherwise
bool substitute(constchar old_string[ ], constchar new_string[ ]);
// Substitutes/replaces all occurrences of old_string with new_string
// Returns number of strings replaced
size_t substituteAll(constchar old_string[ ], constchar 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
friendbooloperator == (const stringArray &bag1, const stringArray& bag2);
// Overloading the != operator
// Friend function to access the private member variables within the bag
friendbooloperator != (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(constchar cur_string[ ]) const;
// Returns number of occurrences of cur_string
size_t occurrences(constchar 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
staticconst size_t DEFAULT_SIZE = 25;
// Typedef stuff
typedefconstchar* 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
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.
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'.
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;
}
}