title: Linux C++ Compiling Error
body: <p>I try to compile my c++ code in Linux using Putty. However I got various errors. I fixed some of them. I think these errors are coming because of my template functions. There may be some memory allocation mistakes too. Can anyone help?</p>
  
  <p>Error:</p>
  
  <pre><code>[fmert@flow sevenbridges]$ gcc -o asd Source.cpp
  In file included from Source.cpp:5:
  dictionary.h:24:7: warning: no newline at end of file
  Source.cpp:194:2: warning: no newline at end of file
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h: In static member function ‘static void std::__fill&lt;&lt;anonymous&gt; &gt;::fill(_ForwardIterator, _ForwardIterator, const _Tp&amp;) [with _ForwardIterator = __gnu_cxx::__normal_iterator&lt;HashTable&lt;Dictionary&gt;::HashEntry*, std::vector&lt;HashTable&lt;Dictionary&gt;::HashEntry, std::allocator&lt;HashTable&lt;Dictionary&gt;::HashEntry&gt; &gt; &gt;, _Tp = HashTable&lt;Dictionary&gt;::HashEntry, bool &lt;anonymous&gt; = false]’:
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:568:   instantiated from ‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&amp;) [with _ForwardIterator = __gnu_cxx::__normal_iterator&lt;HashTable&lt;Dictionary&gt;::HashEntry*, std::vector&lt;HashTable&lt;Dictionary&gt;::HashEntry, std::allocator&lt;HashTable&lt;Dictionary&gt;::HashEntry&gt; &gt; &gt;, _Tp = HashTable&lt;Dictionary&gt;::HashEntry]’
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:330:   instantiated from ‘void std::vector&lt;_Tp, _Alloc&gt;::_M_fill_insert(__gnu_cxx::__normal_iterator&lt;typename std::_Vector_base&lt;_Tp, _Alloc&gt;::_Tp_alloc_type::pointer, std::vector&lt;_Tp, _Alloc&gt; &gt;, size_t, const _Tp&amp;) [with _Tp = HashTable&lt;Dictionary&gt;::HashEntry, _Alloc = std::allocator&lt;HashTable&lt;Dictionary&gt;::HashEntry&gt;]’
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:658:   instantiated from ‘void std::vector&lt;_Tp, _Alloc&gt;::insert(__gnu_cxx::__normal_iterator&lt;typename std::_Vector_base&lt;_Tp, _Alloc&gt;::_Tp_alloc_type::pointer, std::vector&lt;_Tp, _Alloc&gt; &gt;, size_t, const _Tp&amp;) [with _Tp = HashTable&lt;Dictionary&gt;::HashEntry, _Alloc = std::allocator&lt;HashTable&lt;Dictionary&gt;::HashEntry&gt;]’
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:426:   instantiated from ‘void std::vector&lt;_Tp, _Alloc&gt;::resize(size_t, _Tp) [with _Tp = HashTable&lt;Dictionary&gt;::HashEntry, _Alloc = std::allocator&lt;HashTable&lt;Dictionary&gt;::HashEntry&gt;]’
  HashTable.h:107:   instantiated from ‘void HashTable&lt;HashedObj&gt;::rehash() [with HashedObj = Dictionary]’
  HashTable.h:97:   instantiated from ‘void HashTable&lt;HashedObj&gt;::insert(const HashedObj&amp;, int) [with HashedObj = Dictionary]’
  Source.cpp:135:   instantiated from here
  /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:529: error: no match for ‘operator=’ in ‘__first.__gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator* [with _Iterator = HashTable&lt;Dictionary&gt;::HashEntry*, _Container = std::vector&lt;HashTable&lt;Dictionary&gt;::HashEntry, std::allocator&lt;HashTable&lt;Dictionary&gt;::HashEntry&gt; &gt;]() = __value’
  HashTable.h:26: note: candidates are: HashTable&lt;Dictionary&gt;::HashEntry&amp; HashTable&lt;Dictionary&gt;::HashEntry::operator=(HashTable&lt;Dictionary&gt;::HashEntry&amp;)
  </code></pre>
  
  <p>Dictionary.h</p>
  
  <pre><code>ifndef DICTIONARY_H_
  #define DICTINARY_H_
  #include &lt;string&gt;
  #include &lt;climits&gt;
  
  
  class Dictionary{
  public:
      Dictionary():  code1(UINT_MAX), code2(UINT_MAX){}
      Dictionary(int c1, int c2): code1(c1), code2(c2){}
      Dictionary(const Dictionary &amp; rhs): code1(rhs.code1), code2(rhs.code2) {}
  
      bool operator == (const Dictionary &amp; rhs) const;
      bool operator != (const Dictionary &amp; rhs) const;
      Dictionary&amp; operator = (const Dictionary &amp; rhs) {
          code1 = rhs.code1;
          code2 = rhs.code2;
          return *this;
      }
      unsigned code1;
      unsigned code2;
  
  };
  
  unsigned hashing(const Dictionary &amp; d, int size);
  
  
  #endif
  </code></pre>
  
  <p>Dictionary.cpp</p>
  
  <pre><code>#include "dictionary.h"
  
  
  
  bool Dictionary::operator == (const Dictionary &amp; rhs) const{
      return (code1==rhs.code1)&amp;&amp; (code2 == rhs.code2);
  }
  
  bool Dictionary::operator != (const Dictionary &amp; rhs) const{
      return (code1!=rhs.code1)&amp;&amp;(code2!=rhs.code2);
  }
  
  unsigned hashing(const Dictionary &amp; d, int size){
      return  (d.code1 + d.code2) % size;
  }
  </code></pre>
  
  <p>Hashtable.h</p>
  
  <pre><code>#include &lt;vector&gt;
  using namespace std;
  
  template &lt;class HashedObj&gt;
  class HashTable{
  public:
      HashTable( int size = 101 ):
        array( nextPrime( size ) ), currentSize(0){}
  
      HashTable( const HashTable &amp; rhs ):
        array( rhs.array ), currentSize( rhs.currentSize ) { }
  
      const HashedObj &amp; find( const HashedObj &amp; x ) const;
      void findMax(int &amp; maxcount, HashedObj &amp;maxObject);
  
      void insert( const HashedObj &amp; x , int amount);
      void remove( const HashedObj &amp; x );
      void increaseCount(const HashedObj &amp; x);
      int nextPrime(int n);
      bool isPrime(int n);
  
  
      enum EntryType { ACTIVE, EMPTY, DELETED };
  
  private:
      struct HashEntry{
          HashedObj element;
          EntryType info;
          unsigned int count;
  
          HashEntry(const HashedObj &amp; e = HashedObj(), EntryType i = EMPTY, unsigned int c = 0 )
              : element( e ), info( i ),count(c) { }
      };
  
      int findPos( const HashedObj &amp; x ) const;
      vector&lt;HashEntry&gt; array;
      int currentSize;
      const HashedObj ITEM_NOT_FOUND;
  
      bool isActive( int currentPos ) const;
  
      void rehash( );
  };
  
  
  
  
  template &lt;class HashedObj&gt;
  int HashTable&lt;HashedObj&gt;::findPos(const HashedObj &amp; x) const {
      int collisionNum = 0;
      int currentPos = hashing(x, array.size());
  
      while (array[currentPos].info != EMPTY &amp;&amp; array[currentPos].element != x)
          (++currentPos) %= array.size();
  
      return currentPos;
  }
  
  
  
  template &lt;class HashedObj&gt;
  bool HashTable&lt;HashedObj&gt;::isActive(int currentPos) const {
      return array[currentPos].info == ACTIVE;
  }
  
  template &lt;class HashedObj&gt;
  void HashTable&lt;HashedObj&gt;::remove(const HashedObj &amp; x) {
      int currentPos = findPos(x);
      if (isActive(currentPos))
          array[currentPos].info = DELETED;
  }
  
  template &lt;class HashedObj&gt;
  const HashedObj &amp; HashTable&lt;HashedObj&gt;::find(const HashedObj &amp; x) const {
      int currentPos = findPos(x);
      if (isActive(currentPos))
          return array[currentPos].element;
  
      return   ITEM_NOT_FOUND;
  }
  
  template &lt;class HashedObj&gt;
  void HashTable&lt;HashedObj&gt;::insert(const HashedObj &amp; x,int amount) {
      // Insert x as active
      int currentPos = findPos(x);
      if (isActive(currentPos)) {
          for (int i = 0; i &lt; amount; i++) {
              array[currentPos].count++;
          }
          return;
      }
      HashEntry b(x, ACTIVE, amount);
      array[currentPos] = b;
  
      // enlarge the hash table if necessary 
      if (++currentSize &gt;= array.size() / 2)
          rehash();
  }
  
  
  
  template &lt;class HashedObj&gt;
  void HashTable&lt;HashedObj&gt;::rehash() {
      vector&lt;HashEntry&gt; oldArray = array;
  
      // Create new double-sized, empty table
      array.resize(nextPrime(2 * oldArray.size()));
      for (int j = 0; j &lt; array.size(); j++) {
          array[j].count = 0;
          array[j].info = EMPTY;
      }
  
      // Copy table over
      currentSize = 0;
      for (int i = 0; i &lt; oldArray.size(); i++)
          if (oldArray[i].info == ACTIVE) {
              insert(oldArray[i].element,oldArray[i].count);
          }
  }
  
  
  template &lt;class HashedObj&gt;
  void HashTable&lt;HashedObj&gt;::findMax(int &amp;maxcount, HashedObj &amp;maxObject) {
  
      for (int currentPos = 0; currentPos &lt; currentSize; currentPos++) {
          if (isActive(currentPos)) {
              if (array[currentPos].count &gt; maxcount) {
                  maxObject = array[currentPos].element;
                  maxcount = array[currentPos].count;
              }
          }
      }
  
  }
  
  template &lt;class HashedObj&gt;
  int HashTable&lt;HashedObj&gt;::nextPrime(int n) {
      if (n % 2 == 0)
          n++;
  
      for (; !isPrime(n); n += 2)
          ;
  
      return n;
  }
  template &lt;class HashedObj&gt;
  bool HashTable&lt;HashedObj&gt;::isPrime(int n) {
      if (n == 2 || n == 3)
          return true;
  
      if (n == 1 || n % 2 == 0)
          return false;
  
      for (int i = 3; i * i &lt;= n; i += 2)
          if (n % i == 0)
              return false;
  
      return true;
  }
  </code></pre>