title: error LNK2005 - What am I doing wrong here?
body: <p>I've been given some source code (as part of an assignment) that I'm supposed to modify, but I can't get the unmodified version of the code to compile and I'm tearing my hair out. (To be clear - this code is for a school assignment on hash tables, these compile errors are <strong>not</strong> part of the assignment)</p>
  
  <p>I'm using visual studio 2010 to compile. I've been working on this all day and getting absolutely nowhere!</p>
  
  <p>I'm getting a series of "LNK2005" errors:</p>
  
  <pre><code>1&gt;------ Build started: Project: Assignment10, Configuration: Debug Win32 ------
  1&gt;  hashmain.cpp
  1&gt;e:\google drive\cpsc 1160\labs\projects\assignment10\assignment10\hashtable.cpp(40): warning C4018: '&lt;' : signed/unsigned mismatch
  1&gt;hashtable.obj : error LNK2005: "public: __thiscall HashTableSavitch::HashTable::HashTable(void)" (??0HashTable@HashTableSavitch@@QAE@XZ) already defined in hashmain.obj
  1&gt;hashtable.obj : error LNK2005: "public: virtual __thiscall HashTableSavitch::HashTable::~HashTable(void)" (??1HashTable@HashTableSavitch@@UAE@XZ) already defined in hashmain.obj
  1&gt;hashtable.obj : error LNK2005: "private: static int __cdecl HashTableSavitch::HashTable::computeHash(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;)" (?computeHash@HashTable@HashTableSavitch@@CAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in hashmain.obj
  1&gt;hashtable.obj : error LNK2005: "public: bool __thiscall HashTableSavitch::HashTable::containsString(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;)const " (?containsString@HashTable@HashTableSavitch@@QBE_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in hashmain.obj
  1&gt;hashtable.obj : error LNK2005: "public: void __thiscall HashTableSavitch::HashTable::put(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;)" (?put@HashTable@HashTableSavitch@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in hashmain.obj
  1&gt;E:\Google Drive\CPSC 1160\Labs\Projects\Assignment10\Debug\Assignment10.exe : fatal error LNK1169: one or more multiply defined symbols found
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>Here is the code I was given:</p>
  
  <p>hashmain.cpp</p>
  
  <pre><code>// Program to demonstrate use of the HashTable class
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include "listtools.cpp"    // Your compiler may compile separately
  #include "hashtable.h"
  #include "hashtable.cpp"    // Your compiler may compile separately
  using std::string;
  using std::cout;
  using std::endl;
  using HashTableSavitch::HashTable;
  
  int main()
  {
      HashTable h;
  
      cout &lt;&lt; "Adding dog, cat, turtle, bird" &lt;&lt; endl;
      h.put("dog");
      h.put("cat");
      h.put("turtle");
      h.put("bird");
      cout &lt;&lt; "Contains dog? "
          &lt;&lt; h.containsString("dog") &lt;&lt; endl;
      cout &lt;&lt; "Contains cat? "
          &lt;&lt; h.containsString("cat") &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 fish? "
          &lt;&lt; h.containsString("fish") &lt;&lt; endl;
      cout &lt;&lt; "Contains cow? "
          &lt;&lt; h.containsString("cow") &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>hashtable.h</p>
  
  <pre><code>// This is the header file hashtable.h.  This is the interface
  // for the class HashTable, which is a class for a hash table
  // of strings.
  
  #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(); // Initialize empty hash table
  
          // Normally a copy constructor and overloaded assignment
          // operator would be included.  They have been omitted
          // to save space.
  
          virtual ~HashTable();  // Destructor destroys hash table
  
          bool containsString(string target) const;
          // Returns true if target is in the hash table,
          // false otherwise
  
          void put(string s);
          // Adds a new string to the hash table
  
     private:
          Node&lt;string&gt; *hashArray[SIZE];
          static int computeHash(string s);   // Compute hash value for string
  
    }; // HashTable
  } // HashTableSavitch
  #endif // HASHTABLE_H
  </code></pre>
  
  <p>hashtable.cpp</p>
  
  <pre><code>// This is the implementation file hashtble.cpp.
  // This is the implementation of the class HashTable.
  
  #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()
     { 
      for (int i = 0; i &lt; SIZE; i++)
      {
       hashArray[i] = NULL;
      }
     }
  
     HashTable::~HashTable()
     {
       for (int i=0; i&lt;SIZE; i++)
       {
         Node&lt;string&gt; *next = hashArray[i];
         while (next != NULL)
         {
           Node&lt;string&gt; *discard = next;
       next = next-&gt;getLink( );
       delete discard;
         }
       }
     }
  
     int HashTable::computeHash(string s)
     {
      int hash = 0;
      for (int i = 0; i &lt; s.length( ); i++) 
      {
       hash += s[i];
      }
      return hash % SIZE; 
     }
  
     bool HashTable::containsString(string target) const
     { 
      int hash = this-&gt;computeHash(target);
      Node&lt;string&gt;* result = search(hashArray[hash], target);
      if (result == NULL) 
         return false;
      else
         return true;
     }
  
     void HashTable::put(string s)
     {
      int hash = computeHash(s);
      if (search(hashArray[hash], s)==NULL)
      {
        // Only add the target if it's not in the list
        headInsert(hashArray[hash], s);
      }
     }
  
  } // HashTableSavitch
  </code></pre>
  
  <p>listtools.h</p>
  
  <pre><code>//This is the header file listtools.h. This contains type definitions and
  //function declarations for manipulating a linked list to store data of any type T.
  //The linked list is given as a pointer of type Node&lt;T&gt;* which points to the 
  //head (first) node of the list. The implementation of the functions are given
  //in the file listtools.cpp.
  #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&amp; 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);
      //Precondition: The pointer variable head points to
      //the head of a linked list.
      //Postcondition: A new node containing theData
      //has been added at the head of the linked list.
  
      template&lt;class T&gt;
      void insert(Node&lt;T&gt;* afterMe, const T&amp; theData);
      //Precondition: afterMe points to a node in a linked list.
      //Postcondition: A new node containing theData
      //has been added after the node pointed to by afterMe.
  
      template&lt;class T&gt;
      void deleteNode(Node&lt;T&gt;* before);
      //Precondition: The pointers before point to nodes that has 
      //at least one node after it in the linked list. 
      //Postcondition: The node after the node pointed to by before
      //has been removed from the linked list and its storage 
      //returned to the freestore.
  
      template&lt;class T&gt;
      void deleteFirstNode(Node&lt;T&gt;*&amp; head);
      //Precondition: The pointers head points to the first
      //node in a linked list; with at least one node.
      //Postcondition: The node pointed to by head has been removed
      //for the linked list and its storage returned to the freestore.
  
      template&lt;class T&gt;
      Node&lt;T&gt;* search(Node&lt;T&gt;* head, const T&amp; target);
      //Precondition: The pointer head points to the head of a linked list.
      //The pointer variable in the last node is NULL. head (first) node
      //head (first) node has been defined for type T. 
      //(== is used as the criterion for being equal).
      //If the list is empty, then head is NULL.
      //Returns a pointer that points to the first node that 
      //is equal to the target. If no node equals the target, 
      //the function returns NULL.
  }//LinkedListSavitch
  
  #endif //LISTTOOLS_H
  </code></pre>
  
  <p>listtools.cpp</p>
  
  <pre><code>//This is the implementation file listtools.cpp. This file contains 
  //function definitions for the functions declared in listtools.h.
  
  #include &lt;cstddef&gt;
  #include "listtools.h"
  
  namespace LinkedListSavitch
  {
      template&lt;class T&gt;
      void headInsert(Node&lt;T&gt;*&amp; head, const T&amp; theData)
      {
          head = new Node&lt;T&gt;(theData, head);
      }
  
      template&lt;class T&gt;
      void insert(Node&lt;T&gt;* afterMe, const T&amp; theData)
      {
          afterMe-&gt;setLink(new Node&lt;T&gt;(theData, afterMe-&gt;getLink( )));
      }
  
      template&lt;class T&gt;
      void deleteNode(Node&lt;T&gt;* before)
      {
          Node&lt;T&gt; *discard;
          discard = before-&gt;getLink( );
          before-&gt;setLink(discard-&gt;getLink( ));
          delete discard;
      }
  
      template&lt;class T&gt;
      void deleteFirstNode(Node&lt;T&gt;*&amp; head)
      {
          Node&lt;T&gt; *discard;
          discard = head;
          head = head-&gt;getLink( );
          delete discard;
      }
  
      //Uses cstddef:
      template&lt;class T&gt;
      Node&lt;T&gt;* search(Node&lt;T&gt;* head, const T&amp; target)
      {
          Node&lt;T&gt;* here = head;
  
          if (here == NULL) //if empty list
          {
              return NULL;
          }
          else
          {
              while (here-&gt;getData( ) != target &amp;&amp; here-&gt;getLink( ) != NULL)
                  here = here-&gt;getLink( );
  
              if (here-&gt;getData( ) == target)
                  return here;
              else
                  return NULL;
          }
      }
  
  }//LinkedListSavitch
  </code></pre>
  
  <p>I think that this is way out of my depth, I've poured over similar problems/solutions here and anywhere else I could find on google, but I'm completely stumped.</p>
  
  <p><strong>Edit:</strong>
  As per the request of Arcinde, I commented out <code>#include "hashtable.cpp"</code> in hashmain.cpp like so:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include "listtools.cpp"    // Your compiler may compile separately
  #include "hashtable.h"
  //#include "hashtable.cpp"  // Your compiler may compile separately
  using std::string;
  using std::cout;
  using std::endl;
  using HashTableSavitch::HashTable;
  </code></pre>
  
  <p>which produces the following errors:</p>
  
  <pre><code>1&gt;------ Build started: Project: Assignment10, Configuration: Debug Win32 ------
  1&gt;  hashmain.cpp
  1&gt;hashtable.obj : error LNK2019: unresolved external symbol "class LinkedListSavitch::Node&lt;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; &gt; * __cdecl LinkedListSavitch::search&lt;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; &gt;(class LinkedListSavitch::Node&lt;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; &gt; *,class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;)" (??$search@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@LinkedListSavitch@@YAPAV?$Node@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@0@PAV10@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: bool __thiscall HashTableSavitch::HashTable::containsString(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;)const " (?containsString@HashTable@HashTableSavitch@@QBE_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  1&gt;hashtable.obj : error LNK2019: unresolved external symbol "void __cdecl LinkedListSavitch::headInsert&lt;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; &gt;(class LinkedListSavitch::Node&lt;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; &gt; * &amp;,class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;)" (??$headInsert@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@LinkedListSavitch@@YAXAAPAV?$Node@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@0@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: void __thiscall HashTableSavitch::HashTable::put(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;)" (?put@HashTable@HashTableSavitch@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  1&gt;E:\Google Drive\CPSC 1160\Labs\Projects\Assignment10\Debug\Assignment10.exe : fatal error LNK1120: 2 unresolved externals
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>