0

I have checked similar questions and I tried the suggestions but it was in vain I guess I have a quite different problem here So, I am trying to implement a HashTables Class. I have three files in my project: "HashTables.h" , "HashTables.cpp" , and the main to test the class But I am having problems with my Hashtables.cpp file

Here is the code of my header file:

<pre>  //File: HashTables.h
// Hashtables class definition 

#ifndef HashTables_H
#define HashTables_H

template <class keyType, class dataType>
class HashTables
{

public:
    HashTables(int nelements = 11) // Constructor with argument, size is nelements, default is 11
    {
        MaxSize= nelements;
        HT = new NamePass[nelements];
        hsize = 0;
    }  

    ~HashTables()  //Deconstructor
    { 
        delete [] HT;
    }

    bool TableIsEmpty(); //Return true if table is empty
    bool TableIsFull() ; // Return true if table is full
    int Occupancy() ; // to return the current occupancy of the Table 
    void EmptyTable(const keyType&); // Empty all slots
    void Insert(const keyType, const dataType); //Insert an element in the HashTable
    bool Check(const keyType, const dataType) ;// Checks if the pair of key-data matches any slot of the hashtable

private:
    struct NamePass
    {
        keyType key;
        dataType data;
    };

    NamePass* HT; //HashTable head
    int MaxSize; //the size of the HashTable
    int hsize; //the number of the hashed elements inside the HashTable
    keyType Empty;
    int h;
    int Hash(const keyType&) ; // outputs the position that the key entered should be placed in in the table

};

#endif

#include"hashtables.cpp"
 </pre>

The code of my HashTables.cpp file:

<pre> 

// File:HashTable.cpp
// HashTable Class implementation file

#include <iostream>
using namespace std;


// return True if Table is empty
template<class keyType, class dataType>
bool HashTables<keyType, dataType>::TableIsEmpty() const
{
    return (hsize == 0);
}

// return True if Table is full
template<class keyType, class dataType>
bool HashTables<keyType, dataType>::TableIsFull() const
{
    return (hsize == MaxSize);
}

// to return the current occupancy of the Table 
template<class keyType, class dataType>
int HashTables<keyType, dataType>::Occupancy() const
{   
    return hsize;   
}

// Empty all slots
template<class keyType, class dataType>
void HashTables<keyType, dataType>::EmptyTable(const keyType &k) 
{
    Empty = k;
    for(int i = 0; i < MaxSize; i++)
        T[i].key = Empty;
    h = -1;  hsize = 0;
}


// insert key and data at a hashed slot
template<class keyType, class dataType>
bool HashTables<keyType, dataType>::Insert(const keyType &k, const dataType &d)
{   
    if (!TableIsFull()) 
    { 
        h = hash(k); 
        while(HT[h].key != Empty)
            h = (h+1) % MaxSize;
        HT[h].key = k;
        HT[h].data = d;
        hsize++;
        return true;
    }
    else return false;
}

// Search the Table for the slot that matches key. 
// If found, check if the data given matches the data in the HashTable
template<class keyType, class dataType>
bool HashTables<keyType, dataType>::Check(const keyType &k, const dataType &d) const
{       
    if(!TableIsEmpty()) 
    {
        h = hash(k);
        int start = h;
        for ( ; ; )
        {
            if (HT[h].key == Empty)
                return false;
            if ((k == HT[h].key) && (d == HT[h].data))
                return true;

            h = (h+1) % MaxSize;

            if (h == start)
                return false;
        }
    }
    else return false;
}

// Private Hashing Function - Hashing a string key
template<class keyType, class dataType>
int HashTables<keyType, dataType>::Hash(const keyType &k ) const
{
    int l = k.length();
    int K= (int(k[0]) + int(k[l-1])) / 2 
        return (K % MaxSize)
}

</pre>

And the main to test the class :

    #include<iostream>
    #include<fstream>
    #include<string>


    #include "HashTables.h"

    using namespace std;

    void main()
    {
    HashTables<string, string> SecuredD(22);

    SecuredD.EmptyTable("\0");
    SecuredD.Insert("Chouaib", "mypassword");
    SecuredD.Insert("Kamel", "mypassdwqdword");
    SecuredD.Insert("Hehe", "mypadwqssword");
    SecuredD.Insert("asdw", "mwqdypassword");

    bool checked = SecuredD.Check( "Chouaib", "mypassword");
    cout << endl << checked << endl << endl;

    system("pause");



 </pre> 

This yields the following errors:

Error   1   error C2244: 'HashTables<keyType,dataType>::TableIsEmpty' : unable to match function definition to an existing declaration  c:\users\chouaib\documents\visual studio 2012\projects\cs - assignment 3\cs - assignment 3\hashtables.cpp   13

Error   2   error C2244: 'HashTables<keyType,dataType>::TableIsFull' : unable to match function definition to an existing declaration   c:\users\chouaib\documents\visual studio 2012\projects\cs - assignment 3\cs - assignment 3\hashtables.cpp   20

Error   3   error C2244: 'HashTables<keyType,dataType>::Occupancy' : unable to match function definition to an existing declaration c:\users\chouaib\documents\visual studio 2012\projects\cs - assignment 3\cs - assignment 3\hashtables.cpp   27

Error   4   error C2244: 'HashTables<keyType,dataType>::Insert' : unable to match function definition to an existing declaration    c:\users\chouaib\documents\visual studio 2012\projects\cs - assignment 3\cs - assignment 3\hashtables.cpp   55

Error   5   error C2244: 'HashTables<keyType,dataType>::Check' : unable to match function definition to an existing declaration c:\users\chouaib\documents\visual studio 2012\projects\cs - assignment 3\cs - assignment 3\hashtables.cpp   80

Error   6   error C2244: 'HashTables<keyType,dataType>::Hash' : unable to match function definition to an existing declaration  c:\users\chouaib\documents\visual studio 2012\projects\cs - assignment 3\cs - assignment 3\hashtables.cpp   89
Chouaib
  • 11
  • 2
  • Either put all the template code in the header, or include first the .h file and under it, the .cpp file – DNT Nov 14 '14 at 18:53
  • Thank you @DNT I tried both but I am still getting the exact same errors =( – Chouaib Nov 14 '14 at 19:07

0 Answers0