### 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)
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131`` ``````#include #include #include using namespace std; #include "bag.h" void add_item (Bag& bag); void init_bag (Bag& bag, const string& fname); void help (); void remove_item (Bag& bag); void save_bag (const Bag& bag, const string& fname); void show_bag_detail (const Bag& bag); void show_bag_summary (const Bag& bag); int main () { // Declare and initialize bag. Bag bag1, bag2; init_bag(bag1, "bag1.txt"); init_bag(bag2, "bag2.txt"); // Command loop... cout << endl; help(); string command; do { cout << endl; cout << "bag1: "; show_bag_summary(bag1); cout << "bag2: "; show_bag_summary(bag2); cout << endl; cout << "Command: "; getline(cin, command); if (command == "add1") add_item(bag1); else if (command == "add2") add_item(bag2); else if (command == "remove1") remove_item(bag1); else if (command == "remove2") remove_item(bag2); else if (command == "detail1") show_bag_detail(bag1); else if (command == "detail2") show_bag_detail(bag2); else if (command == "diff12") ;// show_detail(bag1.difference(bag2)) else if (command == "diff21") ;// show_detail(bag1.difference(bag2)) else if (command == "intersect") ;// show_detail(bag1.intersection(bag2)) else if (command == "union") ;// show_detail(bag1.union(bag2)) else if (command != "exit") help(); } while (command != "exit"); // Save bag contents save_bag(bag1, "bag1.txt"); save_bag(bag2, "bag2.txt"); // system("pause"); return 0; } void add_item (Bag& bag){ string item; cout << "Item to add: "; getline(cin, item); if (bag.add(item)) cout << "Item added." << endl; else cout << "Failed to add item." << endl; } void help () { cout << "Valid commands: addN removeN detailN (N = 1 or 2)" << endl; cout << " diff12 diff21 intersect union" << endl; } void init_bag (Bag& bag, const string& fname) { // Open file of items. Exit if file not found. ifstream f(fname.c_str()); if (!f) return; // Get all items from file and add to bag. string item; while (getline(f, item)) bag.add(item); // Close file f.close(); } void remove_item (Bag& bag){ string item; cout << "Item to remove: "; getline(cin, item); // if (bag.remove(item)) // cout << "Item was removed." << endl; // else // cout << "Item was not found." << endl; cout << (bag.remove(item) ? "Item was removed." : "Item was not found.") << endl; } void save_bag (const Bag& bag, const string& fname) { // Open file for saving bag contents. ofstream f(fname.c_str()); if (!f) { cerr << "ERROR: Could not open mybag.txt for output." << endl; return; } // Save bag items for (int i = 0; i < bag.getCurrentSize(); i++) f << bag.getItem(i) << endl; // Close file f.close(); } void show_bag_detail (const Bag& bag) { show_bag_summary(bag); for (int i = 0; i < bag.getCurrentSize(); i++) cout << " - " << bag.getItem(i) << endl; } void show_bag_summary (const Bag& bag) { if (bag.isEmpty()) cout << "The bag is empty." << endl; else { int size = bag.getCurrentSize(); if (size > 1) cout << "There are " << size << " items in the bag." << endl; else cout << "There is 1 item in the bag." << endl; } }``````
 ``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; } ``````