### Need help with creating a function in a class

Hello, I am a computer science student. I am currently working on a "bag" class which is sort of a common sense answer to creating a random class with difference functions. I am attempting to create a "union" function which takes two bags ie: bag1 and bag2, adds all the items in both bags and creates a new bag ie: "bag3". For some reason I keep coming up with problems instead of solutions. Maybe it's the fact I just got done with 2 days of calculus. I don't know. My code is below. Both a main(source) and header file. Any help would be appreciated. Thanks in advance.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 #ifndef BAG_H #define BAG_H const int BAG_CAPACITY = 20; template class Bag { private: int count; // Number of items in the Bag T items[BAG_CAPACITY]; // Items in the Bag // Find index of a given item int findItem (const T& anEntry) const; public: // Initialize the bag Bag () { count = 0; } // Get number of items in the bag int getCurrentSize () const { return count; } // Get an item from the bag using a zero-based index // If index is invalid, returns a default item of type T T getItem (int index) const { return index >= 0 && index < count ? items[index] : T(); } // Determine if bag is empty bool isEmpty () const { return count == 0; } // Add an item to the bag bool add (const T& newEntry); // Determine number of times item appears in the bag int getFrequencyOf (const T& anEntry) const; // Determine if the bag contains the given item bool contains (const T& anEntry) const { return getFrequencyOf(anEntry) > 0; } // Clear all items from the bag void clear () { count = 0; } // Remove an item from the bag bool remove (const T& anEntry); }; template bool Bag::add (const T& newEntry) { if (count == BAG_CAPACITY) return false; items[count++] = newEntry; return true; } template int Bag::getFrequencyOf (const T& anEntry) const { int freq = 0; for (int i = 0; i < count; i++) if (items[i] == anEntry) freq++; return freq; } template int Bag::findItem (const T& anEntry) const { for (int i = 0; i < count; i++) if (items[i] == anEntry) return i; return -1; } template bool Bag::remove (const T& anEntry) { if (!contains(anEntry)) return false; for (int i = findItem(anEntry) + 1; i < count; i++) items[i-1] = items[i]; count--; return true; } template void Bag::union (const T& #endif

Main(source)

 12345678910 template Bag Bag::union (const Bag& rhs) const { Bag bag3; //... Loop over items in *this and add them to bag3 //... Loop over items in rhs and add them to bag3 return bag3; }
Topic archived. No new replies allowed.