Need assistance with overloading operators...AGAIN.

EDIT: Program crashes and I can't figure out why. Objects aren't subtracting...

I'm getting some weird error with my = operator that I'm attempting to overload. I don't even THINK I'm doing this right...I'm supposed to overload the = operator and the + operator of these bags/container classes so it creates a union, IE...

class object1, object2, object3;
object1 = object2 + object3;

Help is GREATLY appreciated;this is due in about 8 hours...

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
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef STRINGARRAY_H_INCLUDED
#define STRINGARRAY_H_INCLUDED

namespace csci2270_assignmentTwo
{
    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;

            // Friend functions
            // Overloading the + operator
            friend stringArray operator + (stringArray& bag1, stringArray& bag2);

            // Overloading the - operator
            //friend stringArray operator - (const stringArray& bag1, const stringArray& bag2) const;

            // Overloading the = operator
            stringArray& operator = (stringArray& bag1, stringArray& bag2);

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


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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "stringArray.h"

using namespace std;

namespace csci2270_assignmentTwo
{
    // 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];
    }



    // Copy constructor
    stringArray::stringArray (const stringArray& source)
    {
        // Assigns all member values to whatever
        count = source.count;
        capacity = source.capacity;

        // std::copy(source.data, source.data + source.count, data);

        data = new stringArray::str[DEFAULT_SIZE];
        for (int i = 0; i < capacity; i++)
        {
            data[i] = source.data[i];
        }
    }



    // Destructor
    stringArray::~stringArray()
    {
        // Frees up memory
        // Uses a for loop to delete each element in the bag
    //	for (int j = 0; j < capacity; j++)
    //	{
    //        delete[] data[j];
    //	}

        // Finally deletes data itself
        delete data;
    }



    // Adds a string into the bag
    // Returns true if successful; false otherwise
    bool stringArray::addString(const char new_string[ ])
    {
        // Check bounds to see if the bag is filled

        // If it attempts to add the string into the full bag, return false
        if ((count + 1) > capacity)
        {
            return false;
        }

        // Otherwise, return true and add the string into the bag
        else
        {
            // Adds the string into the bag
            data[count] = new_string;

            // Increases the number of items in the bag
            count++;

            return true;
        }
    }



    // Friend functions
    // Overloading the + operator
    stringArray operator + (stringArray& bag1, stringArray& bag2)
    {
        // Temporary stringArray
        stringArray temp;
        // Goes through each bag and adds strings in
        for (int i = 0; i < bag1.count; i++)
        {
            temp.data[i] = bag1.data[i];
        }

        for (int j = 0; j < bag2.count; j++)
        {
            temp.data[bag1.count + j] = bag2.data[j];
        }

            return temp;
    }



    // Overloading the = operator for instances like s1 = s2 + s3
    stringArray& stringArray::operator = (stringArray& bag1, stringArray& bag2)
    {
    	count = bag1.count + bag2.count;
    	capacity = bag1.capacity + bag2.capacity;
    	data = new stringArray::str[capacity];

    	return *this;
    }
}
Last edited on
Sorry, I forgot to mention the error I'm having. It says:

error: `csci2270_assignmentTwo::stringArray& csci2270_assignmentTwo::stringArray::operator=
(csci2270_assignmentTwo::stringArray&, csci2270_assignmentTwo::stringArray&)' must take exactly one argument|


...And I'm confused on what's wrong.
Last edited on
... must take exactly one argument


You are in scope of stringArray

 
stringArray& operator = (const stringArray& bag)

Thanks...but I'm REALLY confused on how to implement it if I'm supposed to combine two container classes together.

EDIT: New code:

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
stringArray operator + (const stringArray& bag1, const stringArray& bag2)
    {
        // Temporary stringArray
        stringArray temp;
        // Goes through each bag and adds strings in
        for (int i = 0; i < bag1.count; i++)
        {
            temp.data[i] = bag1.data[i];
        }

        for (int j = 0; j < bag2.count; j++)
        {
            temp.data[bag1.count + j] = bag2.data[j];
        }

            return temp;
    }

stringArray& stringArray::operator = (const stringArray& bag)
    {
    	count = bag.count;
    	capacity = bag.capacity;
    	data = new stringArray::str[capacity];

    	return *this;
    }


My program crashes though...what's wrong?
Last edited on
stringArray doesn't correctly implement copy. You're using the default, which is wrong in this case.
I magically fixed it. I'm having ONE more trouble with ONE more operator overload, though...

I'm trying to overload the - operator so it subtracts the union in both bags and just leaves the bag with the remnants, IE:

object1 = object2 - object3;
(IE, if object2 has [a, b, c] and object3 has [b, c], object1 would be left with [a].)

When I run my program though, I get nothing and it seems to crash...

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
stringArray operator - (stringArray bag1, stringArray bag2)
    {
        // Temporary stringArray
        stringArray temp;
        temp.capacity = bag1.capacity + bag2.capacity;
    	temp.data = new stringArray::str[temp.capacity];

        size_t counter = 0;

        for (int i = 0; i < bag1.count; i++)
            {

                // Sorts bags
                for (int j = 0; j < bag1.count - 1; j++)
                {
                    // First bag
                    // If the left data is "greater" than the right data, swap
                    if (strcmp(bag1.data[i], bag1.data[j]) < 0)
                    {
                        // Temporary variable to hold the item in the two array elements being swapped
                        const char* temporary;
                        temporary = bag1.data[j];
                        bag1.data[j] = bag1.data[j + 1];
                        bag1.data[j + 1] = temporary;
                    }

                    // Second bag
                    if (strcmp(bag2.data[i], bag2.data[j]) < 0)
                    {
                        // Temporary variable to hold the item in the two array elements being swapped
                        const char* temporary;
                        temporary = bag2.data[j];
                        bag2.data[j] = bag2.data[j + 1];
                        bag2.data[j + 1] = temporary;
                    }
                }
            }

        // Goes through each string in the first bag and compares it with the second bag
        for (int i = 0; i < bag1.count; i++)
        {
            // If an item from bag1 exists in bag2, remove it from both bags
            if (bag2.is_string(bag1.data[i]))
            {
                bag1.removeString(bag1.data[i]);
                bag2.removeString(bag2.data[i]);
                counter++;
            }

            // Copies the rest of the data into the bag
            temp.data[i] = bag1.data[i];
        }

        for (int j = 0; j < bag2.count; j++)
        {
            temp.data[bag1.count + j] = bag2.data[j];
        }

        temp.count = bag1.count + bag2.count - (counter * 2);
        return temp;
    }
Last edited on
Sorry, forgot to include these implementations, too:

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
// Returns true if cur_string is in the bag; false if not
    bool stringArray::is_string(const char cur_string[ ]) const
    {
        // Searches through the object
        for (int i = 0; i < capacity; i++)
        {
            if (data[i] == cur_string)
            {
                // Returns true if the string is found in the object
                return true;
            }
        }

        return false;
    }


// Removes one occurrence of old_string from the bag
    // Returns true if successful; false otherwise
    bool stringArray::removeString(const char old_string[ ])
    {
        // Searches for the string
        for (int i = 0; i < capacity; i++)
        {
            if (data[i] == old_string)
            {
                // Shift all of the elements to the left
                for (int index = i; index < capacity; index++)
                {
                    data[index] = data[index+1];
                }

                // Reduces count by 1
                count = count - 1;
                return true;
            }
        }

        // Returns false if the string cannot be found/removed
        return false;
    }
stringArray operator - (stringArray bag1, stringArray bag2) passing in by value?

Have you implemented copy yet?
operator- is quite ... interesting.

First, as implemented, it does NOT do what you say it does.

Let me give a different example:
If A = [z, a, b, c] and B = [b, c], then A - B should be [z, a], but
in your case it would be [a, z]. This is because of the sort. Don't
sort the elements.

(Don't sort them for another reason: you swap characters, but you
compare strings to determine if you should swap.)

With a couple more member functions and constructors, you could
make this function really trivial. For example, if you had an operator+=
that took a char, you could just do:

1
2
3
stringarray temp;

temp += char_to_add_from_bag_1;


instead of managing the internals of the object yourself.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
stringArray& stringArray::operator = (const stringArray& bag)
    {
    	count = bag.count;
    	capacity = bag.capacity;

        // avoid memory leaks
        if  data  set  then
         delete [] data;
        endif
 	
         data = new stringArray::str[capacity];
        //copy the content
        memcpy(data,bag.str, sizeof(str) * capacity);

    	return *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
bool stringArray::removeString(const char old_string[ ])
    {
        // Searches for the string
        for (int i = 0; i < capacity; i++)
        {
            if (data[i] == old_string)
            {
                // Shift all of the elements to the left
                for (int index = i; index < capacity; index++)
                {
                    // zero-based indexing!!!
                    // index+1 > size of array
                    data[index] = data[index+1];
                }

                // Reduces count by 1
                count = count - 1;
                return true;
            }
        }

        // Returns false if the string cannot be found/removed
        return false;
    }
sorry, my mistake
 
memcpy(data, bag.data, sizeof(str) * capacity);
Thanks Moritz--after napping for an hour, I realized that's what my = operator had to look like.

As for the - operator...I'm still really, really confused. I thought I had to sort it because I have to use a for-loop to match each item (data in this case) up and compare them.

And as for implementing copy, what do you mean?

EDIT: I didn't mention that this program involved arrays of chars, did I?
Last edited on
Why don't you use std::vector?


If you inside STL you can use std::algorithm may be stable_sort or what ever.

suggestion

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
class StringArray
{
public:
	typedef const char*				str;
	typedef std::vector<str>	VecStr;

	StringArray(){}
	StringArray(const StringArray& r)
	{ 
		vStr = r.vStr;
	}
	StringArray(str* p, size_t n)
	{
		assign(p,n);
	}

	void assign (str* p, size_t n)
	{
		vStr.resize(n);
		std::copy(p,p+n,vStr.begin());
	}	

	StringArray& operator += (const StringArray& r)
	{		
		vStr.insert(vStr.end(),r.vStr.begin(),r.vStr.end());
		return *this;
	}

	StringArray& operator = (const StringArray& r)
	{
		vStr = r.vStr;
		return *this;
	}	
private:	
	VecStr vStr;
};

StringArray operator + (const StringArray& l, const StringArray& r)
{	
	StringArray tmp(l);

	tmp +=r;

	return tmp;
}
Last edited on
1
2
3
4
For each element E1 in bag1:
  For each element E2 in bag2:
      if( E1 == E2 )
          Add to new bag


It has the same runtime complexity as a bubble sort.


Topic archived. No new replies allowed.