Search code examples
c++algorithmhashtablechaining

no matching function for call to xxx in Hash Table Separate Chaining


Currently I am trying to convert the Binary Search Tree to Hash Table with Separate Chaining algorithm for develop a simple program in DEV-C++.

So far, I've converted the insert,delete,search and display operation function, along with hash function.

item list.txt:

    Item ID  Item Price Item Name
    10001     23.00     Book1 
    10002     24.00     Book2
    10003     31.98     Book3
    10004     41.90     Book4

The part that I'm stuck is I don't know how to insert all items of item list.txt into the hash table.

function to hash text file data:

void fillHashTable(HashTableSeparateChaining hashtable){
ifstream file;
file.open("item list.txt");
if(!file) {
    cout<<" Error opening file. " << endl;}

int item_id;
double price;
string item_name;

Item i;
struct node* A[M];

while(file >> item_id >> price >> item_name)
{
    i.setItem_id(item_id);
    i.setPrice(price);
    i.setItem_name(item_name);

    HashTableSeparateChaining hashtable;
    int hv = hash_function( i.getItem_id());       

    hashtable.insert(&A[hv], &A[hv], i);
}
file.close();}

class HashTableSeparateChaining:

class HashTableSeparateChaining{
  private:
          struct node
          {
                 struct node *next;//SINGLY-LINKED LIST
                 struct node *prev;
                 Item data;
          };

  public:

        void insert(struct node **h, struct node **t, Item i);
        void Delete(struct node **h, struct node **t, int i);
        void display( struct node* h );
        void search( struct node *h, int key );                          
};

insert method:

void HashTableSeparateChaining::insert(struct node **h, struct node **t, Item i){
 struct node *n = new node;

if( *h == NULL ) {
    n->data = i;
    n->next = NULL;
    n->prev = NULL;
    *h = n;
    *t = n;
} else {
    n->data = i;
    n->next = *h;
    n->next->prev = n;
    n->prev = NULL;
    *h = n;
    }}

main:

int main(){
...
struct node* A[M];

bool b;

for( i = 0; i < M; i++ ) {
    A[i] = NULL;
}
fillHashTable(hashtable);
.....
   //I allow user to make insert/delete element to the existing **item.list.text**

So after I run this program, I expect that I can display all the existing data from item list.txt and the data already being hashed. Then user can make insertion or deletion to that text file.

So far, I've got this error when try to compile.

no matching function for call to `HashTableSeparateChaining::insert(node**, node**, Item&)'


Solution

  • After compiling your code with some fixes I'm sure that problem is private node struct. Compiler error that clarifies everything:

    25:6: note: void HashTableSeparateChaining::insert(HashTableSeparateChaining::node**, HashTableSeparateChaining::node**, Item)
    25:6: note:   no known conversion for argument 1 from 'fillHashTable(HashTableSeparateChaining)::node**' to 'HashTableSeparateChaining::node**'
    

    As I already stated beafore in comment node is private local struct of HashTableSeparateChaining class so you shouldn't be able to use it outside of this class like in 3rd snippet. Begin your fixes with moving it outside or make public and refer via HashTableSeparateChaining::node. It will fix this one problem. But there is more. fillHashTable does not take argument by reference and creates local hashtable variables inside loop. IMHO that code should look like this:

    void fillHashTable(HashTableSeparateChaining& hashtable){
    ifstream file;
    file.open("item list.txt");
    if(!file) {
        cout<<" Error opening file. " << endl;}
    
    int item_id;
    double price;
    string item_name;
    
    Item i;
    struct node* A[M];
    
    while(file >> item_id >> price >> item_name)
    {
        i.setItem_id(item_id);
        i.setPrice(price);
        i.setItem_name(item_name);
    
       int hv = hash_function( i.getItem_id());       
       hashtable.insert(&A[hv], &A[hv], i);
    }
    file.close();}
    

    Classes:

    struct node
    {
          struct node *next;//SINGLY-LINKED LIST
          struct node *prev;
          Item data;
    };
    
    class HashTableSeparateChaining{
    public:
    
        void insert(struct node **h, struct node **t, Item i);
        void Delete(struct node **h, struct node **t, int i);
        void display( struct node* h );
        void search( struct node *h, int key );                          
    };