Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Hash Table using chaining, remove method
    text
    copied!<p>I'm implementing a Hash Table in C++ using chaining. The code builds with no errors and the table constucts fine using the insert method. However, when I call the remove method I receive the following error:</p> <p>Unhandled exception at 0x00c53be9 in HashTable.exe: 0xC0000005: Access violation reading location 0x00000000.</p> <p>Hash Entry Code:</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; template &lt;class T&gt; class HashEntry { private: int key; //lookup key T value; //hash data HashEntry&lt;T&gt; *next; public: HashEntry(int key, T value); HashEntry(); int&amp; getKey(); T&amp; getValue(); void setValue(T value); HashEntry&lt;T&gt;* getNext(); void setNext(HashEntry *next); bool operator == (HashEntry&amp; rhs); bool operator != (HashEntry&amp; rhs); HashEntry&lt;T&gt;&amp; operator = (HashEntry&amp; rhs); }; template &lt;class T&gt; HashEntry&lt;T&gt;::HashEntry(int key, T value) { this-&gt;key = key; this-&gt;value = value; this-&gt;next= nullptr; } template &lt;class T&gt; HashEntry&lt;T&gt;::HashEntry() { this-&gt;key = 0; this-&gt;next= nullptr; } template &lt;class T&gt; int&amp; HashEntry&lt;T&gt;::getKey() { return key; } template &lt;class T&gt; T&amp; HashEntry&lt;T&gt;::getValue() { return value; } template &lt;class T&gt; void HashEntry&lt;T&gt;::setValue(T value) { this-&gt;value = value; } template &lt;class T&gt; HashEntry&lt;T&gt;* HashEntry&lt;T&gt;::getNext() { return next; } template &lt;class T&gt; void HashEntry&lt;T&gt;::setNext (HashEntry *next) { this-&gt;next = next; } template &lt;class T&gt; bool HashEntry&lt;T&gt;::operator == (HashEntry&amp; rhs) { return ((this-&gt;getKey() == rhs.getKey()) &amp;&amp; (this-&gt;getValue() == rhs.getValue())); } template &lt;class T&gt; bool HashEntry&lt;T&gt;::operator != (HashEntry&amp; rhs) { return ((this-&gt;getKey() != rhs.getKey()) &amp;&amp; (this-&gt;getValue() != rhs.getValue())); } template &lt;class T&gt; HashEntry&lt;T&gt;&amp; HashEntry&lt;T&gt;::operator = (HashEntry&amp; rhs) { this-&gt;key = rhs.getKey(); this-&gt;value = rhs.getValue(); this-&gt;next = rhs.getNext(); return *this; } </code></pre> <p>Hash Table code:</p> <pre><code>template &lt;class T&gt; class HashTable { private: std::vector&lt;HashEntry&lt;T&gt;&gt; table; static const int DEFAULT_TABLE_SIZE = 128; int TABLE_SIZE; public: HashTable(); void insert(int key, T value); void remove(int key); void get(int key); ~HashTable(); }; template &lt;class T&gt; HashTable&lt;T&gt;::HashTable() { TABLE_SIZE = DEFAULT_TABLE_SIZE; table.resize(TABLE_SIZE); } </code></pre> <p>Remove Method Code:</p> <pre><code>template &lt;class T&gt; void HashTable&lt;T&gt;::remove(int key) { int hashFunc = (key % TABLE_SIZE); if (table[hashFunc] != HashEntry&lt;T&gt;()) { HashEntry&lt;T&gt; prevEntry = HashEntry&lt;T&gt;(); HashEntry&lt;T&gt; entry = table[hashFunc]; while (entry.getNext() != nullptr &amp;&amp; entry.getKey() != key) { prevEntry = entry; entry = *entry.getNext(); } if (entry.getKey() == key) { if (prevEntry == HashEntry&lt;T&gt;()) { HashEntry&lt;T&gt; nextEntry = *entry.getNext(); //Where the exception is thrown entry = HashEntry&lt;T&gt;(); table[hashFunc] = nextEntry; } else { HashEntry&lt;T&gt; *next = entry.getNext(); entry = HashEntry&lt;T&gt;(); prevEntry.setNext(next); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload