0

When I declare a new Hashtable such as:
String a = null;
HashTable<string> = new HashTable(a, 100);

It tells me HashTable is not a type.

#ifndef HASHTABLE_H
        #define HASHTABLE_H
#include <iostream>
#include <vector>
#include <list>
using namespace std;
template <typename HashedObj>
 class HashTable
 {
 public:
 explicit HashTable( const HashedObj & notFound, int size = 101 );
 HashTable( const HashTable & rhs ): item( rhs.item ), theLists( rhs.theLists ) { }
 void makeEmpty();
 void insert(const HashedObj &x);
 void remove(const HashedObj &x);
 bool contains(const HashedObj &x)const;
 const HashedObj & find( const HashedObj & x ) const;
private:
const HashedObj item;
int size;
vector<list<HashedObj> > theLists;
};
template <typename HashedObj>
int hash(const HashedObj &x, int tablesize);
#endif
//
#include "hashTable.h"
#include <iostream>
#include <list>
#include <string>
#include <math.h>
#include <iterator>
bool checkforlargeprime (int num);

bool checkprime (int nn) {
    if (nn > 100) {
        return checkforlargeprime(nn);
    } else {
        int k=2;
        while (k < nn) {
            int sd = nn%k;
            if ( sd == 0) {
                return false;
            }
            k++;
        }
    }
    return true;
}
int getNextPrimeNumber (int num) {
    int nam = num+1;
    bool das = true;
    while ( das == true ) {
        if (checkprime(nam))
            das = false;
        else
            nam = nam+1;
    }
    return nam;
}
bool checkforlargeprime (int num) {
    if (num > 100) {
        int sss = ((int)(sqrt((double)num)))+1;
        int pn = 2;

        while (pn < sss) {
            if (num%pn == 0) {
                return false;
            }
            pn = getNextPrimeNumber(pn);
        }
        return true;
    } else {
        return false;
    }
}

template <class HashedObj>
        HashTable<HashedObj>::HashTable( const HashedObj & notFound, int size )
          : item( notFound ), theLists( getNextPrimeNumber( size ) )
        {
        }
template <class HashedObj>
        void HashTable<HashedObj>::insert( const HashedObj & x )
        {
            list<HashedObj> &whichList = theLists[hash(x, theLists.size())];
            typename std::list<HashedObj>::iterator itr = whichList.find(x);

            if( itr.isPastEnd( ) )
                whichList.insert( x, whichList.zeroth( ) );
        }
template <class HashedObj>
        void HashTable<HashedObj>::remove(const HashedObj &x )
        {
           theLists[hash( x, theLists.size( ))].remove(x);
        }
template <class HashedObj>
        bool HashTable<HashedObj>::contains(const HashedObj &x )const
        {
           const list<HashedObj> & whichList = theLists[hash(x)];
		   return find(whichList.begin(),whichList.end(),x)!=whichList.end();
        }
		
template <class HashedObj>
        const HashedObj &HashTable<HashedObj>::find( const HashedObj &x ) const
        {
            typename std::list<HashedObj>::iterator itr;
            itr = theLists[ hash( x, theLists.size( ) ) ].find( x );
            if( itr.isPastEnd( ) )
                return item;
            else
                return itr.retrieve( );
        }
template <class HashedObj>
        void HashTable<HashedObj>::makeEmpty( )
        {
            for( int i = 0; i < theLists.size( ); i++ )
                theLists[ i ].makeEmpty( );
        }
	
        int hash( const string &key, int tableSize)
        {
            int hashVal = 0;
			for(int i = 0; i<key.length();  i++)
			hashVal = 37*hashVal+key[i];
			hashVal %= tableSize;
			if(hashVal<0)
			hashVal += tableSize;
			return hashVal;
        }
3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by firstPerson
0

maybe you want :

HashTable<string> = new HashTable<string>(a, 100);

[edit]
Oh, you need to provide the template class and its definition in the same file.
[/edit]

Edited by firstPerson: n/a

0

Thanks, did that now, but now it gives me a compiler error of
undefined reference to `HashTable<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::HashTable(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
collect2: ld returned 1 exit status

0

You should either include "hashTable.cpp", or have the class declaration and implementation in the same file(as firstPerson already pointed out)

0

Im sorry im not sure what your edit exactly means

Put the definition in your Hastable with the class.

//Idea works

#ifndef FOO_H_
#define FOO_H_

template<class T>
class Foo {
public : 
 Foo() { cout <<" Foo ctor\n"; }
 void fooIt();
};

template<typename T>
void Foo<T>::fooIt() { cout << " Fooing it now\n"; }
#endif
//main.cpp
#include"foo.h"
int main() { return 0; }

//Idea does not works

#ifndef FOO_H_
#define FOO_H_

template<class T>
class Foo {
public : 
 Foo() { cout <<" Foo ctor\n"; }
 void fooIt();
};
#endif
//main.cpp
#include"Foo.h"

template<typename T>
void Foo<T>::fooIt() { cout << " Fooing it now\n"; }

int main() { return 0; }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.