title: C++ multiple definition first defined here
body: <p>I'm typing out an example program from Absolute C++ and it keeps giving me this error:</p>
  
  <pre><code>/tmp/ccJfp4DM.o: In function `HashTableSavitch::HashTable::HashTable()':
  hashtableimp.cpp:(.text+0x0): multiple definition of `HashTableSavitch::HashTable::HashTable()'
  /tmp/ccHuZlFl.o:hashtable.cpp:(.text+0x0): first defined here
  /tmp/ccJfp4DM.o: In function `HashTableSavitch::HashTable::HashTable()':
  hashtableimp.cpp:(.text+0x0): multiple definition of `HashTableSavitch::HashTable::HashTable()'
  /tmp/ccHuZlFl.o:hashtable.cpp:(.text+0x0): first defined here
  /tmp/ccJfp4DM.o: In function `HashTableSavitch::HashTable::~HashTable()':
  hashtableimp.cpp:(.text+0x16): multiple definition of `HashTableSavitch::HashTable::~HashTable()'
  /tmp/ccHuZlFl.o:hashtable.cpp:(.text+0x16): first defined here
  /tmp/ccJfp4DM.o: In function `HashTableSavitch::HashTable::~HashTable()':
  hashtableimp.cpp:(.text+0x16): multiple definition of `HashTableSavitch::HashTable::~HashTable()'
  /tmp/ccHuZlFl.o:hashtable.cpp:(.text+0x16): first defined here
  /tmp/ccJfp4DM.o: In function `HashTableSavitch::HashTable::~HashTable()':
  hashtableimp.cpp:(.text+0x44): multiple definition of `HashTableSavitch::HashTable::~HashTable()'
  /tmp/ccHuZlFl.o:hashtable.cpp:(.text+0x44): first defined here
  /tmp/ccJfp4DM.o: In function `HashTableSavitch::HashTable::computeHash(std::string)':
  hashtableimp.cpp:(.text+0x6a): multiple definition of `HashTableSavitch::HashTable::computeHash(std::string)'
  /tmp/ccHuZlFl.o:hashtable.cpp:(.text+0x6a): first defined here
  /tmp/ccJfp4DM.o: In function `HashTableSavitch::HashTable::containsString(std::string) const':
  hashtableimp.cpp:(.text+0x7a): multiple definition of `HashTableSavitch::HashTable::containsString(std::string) const'
  /tmp/ccHuZlFl.o:hashtable.cpp:(.text+0x7a): first defined here
  /tmp/ccJfp4DM.o: In function `HashTableSavitch::HashTable::put(std::string)':
  hashtableimp.cpp:(.text+0x8e): multiple definition of `HashTableSavitch::HashTable::put(std::string)'
  /tmp/ccHuZlFl.o:hashtable.cpp:(.text+0x8e): first defined here
  collect2: error: ld returned 1 exit status
  Makefile:2: recipe for target 'hashTable' failed
  make: *** [hashTable] Error 1
  </code></pre>
  
  <p>I've reduced the program down to its pure basics but I still can't figure out what's going on. Can anyone help?</p>
  
  <p>Makefile:</p>
  
  <pre><code>hashTable: listtools.h listtools.cpp hashtable.h hashtable.cpp hashtableimp.cpp 
      g++ listtools.h listtools.cpp hashtable.h hashtable.cpp hashtableimp.cpp -o hashTable
  </code></pre>
  
  <p>hashtable.h</p>
  
  <pre><code>#ifndef HASHTABLE_H
  #define HASHTABLE_H
  
  #include &lt;string&gt;
  #include "listtools.h"
  
  using LinkedListSavitch::Node;
  using std::string;
  
  namespace HashTableSavitch {
      const int SIZE = 10;
      class HashTable {
      public:
          HashTable();
          virtual ~HashTable();
          bool containsString(string target) const;
          void put(string s);
      private:
          Node&lt;string&gt; *hashArray[SIZE];
          static int computeHash(string s);
      };
  }
  
  #endif
  </code></pre>
  
  <p>hashtable.cpp</p>
  
  <pre><code>#include &lt;string&gt;
  #include "listtools.h"
  #include "hashtable.h"
  
  using LinkedListSavitch::Node;
  using LinkedListSavitch::search;
  using LinkedListSavitch::headInsert;
  using std::string;
  
  namespace HashTableSavitch {
      HashTable::HashTable() {
  
      }
  
      HashTable::~HashTable() {
  
      }
  
      int HashTable::computeHash(string s) {
          return 0;
      }
      bool HashTable::containsString(string target) const {
          return true;
      }
  
      void HashTable::put(string s) {
  
      }
  } // HashTableSavitch
  </code></pre>
  
  <p>hashtableimp.cpp</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include "hashtable.h"
  #include "listtools.cpp"
  #include "hashtable.cpp"
  using std::string;
  using std::cout;
  using std::endl;
  using HashTableSavitch::HashTable;
  
  int main() {
      HashTable h;
      cout &lt;&lt; "Adding cat, dog, turtle, and bird" &lt;&lt; endl;
      h.put("cat");
      h.put("dog");
      h.put("turtle");
      h.put("bird");
  
      cout &lt;&lt; "Contains cat? " &lt;&lt; h.containsString("cat") &lt;&lt; endl;
      cout &lt;&lt; "Contains dog? " &lt;&lt; h.containsString("dog") &lt;&lt; endl;
      cout &lt;&lt; "Contains turtle? " &lt;&lt; h.containsString("turtle") &lt;&lt; endl;
      cout &lt;&lt; "Contains bird? " &lt;&lt; h.containsString("bird") &lt;&lt; endl;
      cout &lt;&lt; "Contains cow? " &lt;&lt; h.containsString("cow") &lt;&lt; endl;
      cout &lt;&lt; "Contains fish? " &lt;&lt; h.containsString("fish") &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>listtools.h</p>
  
  <pre><code>#ifndef LISTTOOLS_H
  #define LISTTOOLS_H
  
  namespace LinkedListSavitch {
      template&lt;class T&gt;
      class Node {
      public:
          Node(const T&amp; theData, Node&lt;T&gt;* theLink) : data(theData), link(theLink) {}
          Node&lt;T&gt;* getLink() const {return link;}
          const T getData() const {return data;}
          void setData(const T&amp; theData) {data = theData;}
          void setLink(Node&lt;T&gt;* pointer) {link = pointer;}
      private:
          T data;
          Node&lt;T&gt; *link;
      };
  
      template&lt;class T&gt;
      void headInsert(Node&lt;T&gt;*&amp; head, const T&amp; theData);
  
      template&lt;class T&gt;
      void insert(Node&lt;T&gt;* afterMe, const T&amp; theData);
  
      template&lt;class T&gt;
      void deleteNode(Node&lt;T&gt;* before);
  
      template&lt;class T&gt;
      void deleteFirstNode(Node&lt;T&gt;*&amp; head);
  
      template&lt;class T&gt;
      Node&lt;T&gt;* search(Node&lt;T&gt;* head, const T&amp; target);
  } // LinkedListSavitch
  
  #endif
  </code></pre>