I have an assignment (below) where I need to modify a program so that it does not allow duplicate elements. I am given 4 files to work with and I can't even begin to modify them because I am getting some kind of template errors after trying to set up the files.
Error:
Severity Code Description Project File Line Suppression State
Error (active) E0864 vector is not a template Project28
Textbook If anyone wants to see it.
https://fall14cs.files.wordpress.com/2016/03/data-abstraction-and-problem-soving-with-c.pdf
Assignment:
The problem refers to Programming Problem 5 of Chapter 1, which you didn't do, but it was just writing out specifications for a "Set" ADT, which is just a slight variation on the "Bag" ADT. Your task this week is to write an "ArraySet" class to implement the "Set" ADT. You don't need to write out the specifications.
The vast majority of your code will be copied directly from the ArrayBag class of chapter 3. You just need to make minor adjustments to reflect the fact that duplicate elements are not allowed. (Note that you will receive 0 if your submitted code allows duplicate elements, since this is pretty much the only thing we are changing.) Also, we won't have a frequencyOf() member function.
Your ArraySet class must be derived from a "SetInterface" abstract class. There's almost no work for you to do here. Just copy the "BagInterface" class from chapter 1. None of the code in that abstract class will need to be modified, except that the frequencyOf() member function will be removed. If you find the part about deriving the class from "SetInterface" confusing, be sure to ask about it in the discussion!
Here is the source code from the text that you are to use as a starting point:
ArrayBag.cpp
ArrayBag.h
BagInterface.h
bagtester.cpp
4 given files:
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
#ifndef ARRAY_BAG_
#define ARRAY_BAG_
template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
private:
static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
// Returns either the index of the element in the array items that
// contains the given target or -1, if the array does not contain
// the target.
int getIndexOf(const ItemType& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector<ItemType> toVector() const;
};
#endif
#ifndef BAG_INTERFACE_
#define BAG_INTERFACE_
#include <vector>
template<class ItemType>
class BagInterface
{
public:
virtual int getCurrentSize() const = 0;
virtual bool isEmpty() const = 0;
virtual bool add(const ItemType& newEntry) = 0;
virtual bool remove(const ItemType& anEntry) = 0;
virtual void clear() = 0;
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
virtual bool contains(const ItemType& anEntry) const = 0;
virtual std::vector<ItemType> toVector() const = 0;
virtual ~BagInterface() { }
};
#include <iostream>
#include "ArrayBag.h"
#include "BagInterface.h"
#include <vector>
template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
}
template<class ItemType>
ArrayBag<ItemType>::ArrayBag() : itemCount(0), maxItems(DEFAULT_CAPACITY)
{
}
template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return itemCount;
}
template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
return itemCount == 0;
}
template<class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const
{
std::vector<ItemType> bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
}
template <class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType & anEntry) const
{
bool isFound = false;
int curIndex = 0; // Current array index
while (!isFound && (curIndex < itemCount))
{
isFound = (anEntry == items[curIndex]);
if (!isFound)
curIndex++; // Increment to next entry
}
return isFound;
}
template<class ItemType>
void ArrayBag<ItemType>::clear()
{
itemCount = 0;
} // end clear
template<class ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType & target) const
{
bool isFound = false;
int result = -1;
int searchIndex = 0;
while (!isFound && (searchIndex < itemCount))
{
isFound = (items[searchIndex] == target);
if (isFound)
{
result = searchIndex;
}
else
{
searchIndex++;
}
}
return result;
}
template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType & anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
}
return canRemoveItem;
}
template<class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType & anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
}
curIndex++;
}
return frequency;
}
#include <iostream>
#include <string>
#include "ArrayBag.h"
#include "BagInterface.h"
#include <vector>
using std::cout;
using std::endl;
using std::string;
void displayBag(ArrayBag<std::string>& bag)
{
cout << "The bag contains " << bag.getCurrentSize()
<< " items:" << endl;
std::vector<string> bagItems = bag.toVector();
int numberOfEntries = (int)bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout << bagItems[i] << " ";
}
cout << endl << endl;
}
void bagTester(ArrayBag<string>& bag)
{
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
displayBag(bag);
std::string items[] = { "one", "two", "three", "four", "five", "one" };
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bag.add(items[i]);
} // end for
displayBag(bag);
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize()
<< "; should be 6" << endl;
cout << "Try to add another entry: add(\"extra\") returns "
<< bag.add("extra") << endl;
cout << "contains(\"three\"): returns " << bag.contains("three")
<< "; should be 1 (true)" << endl;
cout << "contains(\"ten\"): returns " << bag.contains("ten")
<< "; should be 0 (false)" << endl;
cout << "getFrequencyOf(\"one\"): returns "
<< bag.getFrequencyOf("one") << " should be 2" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "getFrequencyOf(\"one\"): returns "
<< bag.getFrequencyOf("one") << " should be 1" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 1 (true)" << endl;
cout << "remove(\"one\"): returns " << bag.remove("one")
<< "; should be 0 (false)" << endl;
cout << endl;
displayBag(bag);
cout << "After clearing the bag, ";
bag.clear();
cout << "isEmpty: returns " << bag.isEmpty()
<< "; should be 1 (true)" << endl;
}
int main()
{
ArrayBag<std::string> bag;
cout << "Testing the Array-Based Bag:" << endl;
cout << "The initial bag is empty." << endl;
bagTester(bag);
cout << "All done!" << endl;
return 0;
}
|