Data Structures. Need help debugging my code. My assignment is to make an implementation of the Bag ADT using a linked data structure and using cells to represent available memory. I have edited the ArrayLinkedBag.cpp file and the ArrayLinkedBag.h file. I've gotten this far but for some reason my program aborts when trying to make test and I don't know why. Any help fixing my code would be appreciated. I am attaching my error message as well.
So I'm trying to implement the Bag ADT in C++ using a linked data structure with an array of cells. I've edited the ArrayLinkedBag.cpp and the header file however I keep running into weird errors and I was wondering if anyone can help me figure out where I went wrong. When I try to make test, the whole thing aborts for some reason and I can't pinpoint the error. I've tried everything to pinpoint the problem including rewriting everything and testing the lines one by one but it just isn't working. Any help would be appreciated.
ArrayLinkedBag.cpp ``` /** Implementation file for the class ArrayLinkedBag. @file ArrayLinkedBag.cpp */
#include "ArrayLinkedBag.h" #include
template ArrayLinkedBag::ArrayLinkedBag(): head(-1), free(0), itemCount(0) { //create free list for (int i = 0; i ){"a",i+1}; }
} // end default constructor
template int ArrayLinkedBag::getCurrentSize() const { return itemCount; } // end getCurrentSize
template bool ArrayLinkedBag::isEmpty() const { return itemCount == 0; } // end isEmpty
template bool ArrayLinkedBag::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount ){newEntry,head}; head=free; itemCount++; free=newFree; } // end if
return hasRoomToAdd; } // end add
template bool ArrayLinkedBag::remove(const ItemType& anEntry) { int locatedIndex = getIndexOf(anEntry); bool canRemoveItem = !isEmpty() && (locatedIndex > -1); if (canRemoveItem) { itemCount--; cells[locatedIndex] = (Cell){NULL, free}; free=locatedIndex; } // end if
return canRemoveItem; } // end remove
template void ArrayLinkedBag::clear() { //reset free list for (int i = 0; i ){"a",i+1}; } head=-1; free=0; itemCount = 0; } // end clear
template bool ArrayLinkedBag::contains(const ItemType& anEntry) const { return getIndexOf(anEntry) > -1; } // end contains
template int ArrayLinkedBag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int curIndex = 0; // Current array index while (curIndex curIndex++; // Increment to next entry } // end while
return frequency; } // end getFrequencyOf
template std::vector ArrayLinkedBag::toVector() const { std::vector bagContents; for (int i = 0; i return bagContents; } // end toVector
// private template int ArrayLinkedBag::getIndexOf(const ItemType& target) const { bool found = false; int result = -1; int searchIndex = 0;
// If the bag is empty, itemCount is zero, so loop is skipped while (!found && (searchIndex
return result; } // end getIndexOf
```
ArrayLinkedBag.h ``` /** Header file for an array-based linked list implementation of the ADT bag. @file ArrayLinkedBag.h */
#ifndef ARRAY_LINKED_BAG_ #define ARRAY_LINKED_BAG_
#include "BagInterface.h"
template class ArrayLinkedBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 6;
template struct Cell { T item; int next; };
Cell cells[DEFAULT_CAPACITY]; int head; int itemCount; // Current count of bag items int free; // 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: ArrayLinkedBag(); 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; std::vector toVector() const;
};
#include "ArrayLinkedBag.cpp" #endif
```
Error Message ``` g++ -Wall -Wextra -g src/test_driver.cpp -o build/test_driver In file included from src/test_driver.cpp:7: src/ArrayLinkedBag.cpp: In instantiation of ArrayLinkedBag::ArrayLink edBag() [with ItemType = std::__cxx11::basic_string]: src/test_driver.cpp:91:48: required from here src/ArrayLinkedBag.h:24:9: warning: ArrayLinkedBag<:__cxx11::basic_string har> >::free1 will be initialized after [-Wreorder] int free1; ^~~~~ src/ArrayLinkedBag.h:22:9: warning: int ArrayLinkedBag<:__cxx11::basic_st ring> >::head [-Wreorder] int head; ^~~~ In file included from src/ArrayLinkedBag.h:43, from src/test_driver.cpp:7: src/ArrayLinkedBag.cpp:8:1: warning: when initialized here [-Wreorder] ArrayLinkedBag::ArrayLinkedBag():free1(0), head(0), itemCount(0){ ^~~~~~~~~~~~~~~~~~~~~~~~ [username testassignment3]$ make test terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid /bin/sh: line 1: 98661 Aborted (core dumped) ./build/test_driver ARRAY_LI NKED > output/array_linked.out make: *** [test] Error 134
```
g++ -Wall -Wextra -g src/test_driver.cpp -o build/test_driver In file included from src/test_driver.cpp:7: src/ArrayLinkedBag.cpp: In instantiation of 'ArrayLinkedBag::ArrayLinkedBag() [with Item Type = std:: cxxll::basic_string]': src/test_driver.cpp:92:48: required from here src/ArrayLinkedBag.h:24:9: warning: 'ArrayLinkedBag<: _cxxll::basic_string> >:: free' will be initialized after [-Wreorder] int free; src/ArrayLinkedBag.h:23:9: warning: Count' [-Wreorder] int itemCount; 'int ArrayLinkedBag<:__cxxll::basic_string> >::item // Current count of bag items In file included from src/ArrayLinkedBag.h:43, from src/test_driver.cpp:7: src/ArrayLinkedBag.cpp:8:1: warning: when initialized here [-Wreorder] ArrayLinkedBag::ArrayLinkedBag(): head (-1), free (0), itemCount (0) assignment3] $ make test terminate called after throwing an instance of 'std::logic_error' what(): basic_string: : _M_construct null not valid /bin/sh: line 1: 91737 Aborted (core dumped) ./build/test_driver ARRAY_LINKED > ou tput/array_linked.out make: *** [test] Error 134 g++ -Wall -Wextra -g src/test_driver.cpp -o build/test_driver In file included from src/test_driver.cpp:7: src/ArrayLinkedBag.cpp: In instantiation of 'ArrayLinkedBag::ArrayLinkedBag() [with Item Type = std:: cxxll::basic_string]': src/test_driver.cpp:92:48: required from here src/ArrayLinkedBag.h:24:9: warning: 'ArrayLinkedBag<: _cxxll::basic_string> >:: free' will be initialized after [-Wreorder] int free; src/ArrayLinkedBag.h:23:9: warning: Count' [-Wreorder] int itemCount; 'int ArrayLinkedBag<:__cxxll::basic_string> >::item // Current count of bag items In file included from src/ArrayLinkedBag.h:43, from src/test_driver.cpp:7: src/ArrayLinkedBag.cpp:8:1: warning: when initialized here [-Wreorder] ArrayLinkedBag::ArrayLinkedBag(): head (-1), free (0), itemCount (0) assignment3] $ make test terminate called after throwing an instance of 'std::logic_error' what(): basic_string: : _M_construct null not valid /bin/sh: line 1: 91737 Aborted (core dumped) ./build/test_driver ARRAY_LINKED > ou tput/array_linked.out make: *** [test] Error 134