在 Hash Table Separate Chaining 中没有调用 xxx 的匹配函数

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

目前,我正在尝试使用分离链接算法将二叉搜索树转换为散列 Table,以便在 DEV-C++ 中开发一个简单的程序。

至此,我已经转换了插入、删除、搜索和显示操作函数,以及哈希函数。

项目list.txt:

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

我卡住的部分是我不知道如何插入 item list.txt 的所有项目进入散列 table.

散列文本文件数据的函数:

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 散列TableSeparateChaining:

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 );                          
};

插入方法:

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;
    }}

主要内容:

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**

所以在我 运行 这个程序之后,我希望我可以显示来自 item list.txt 的所有现有数据以及已经被散列的数据.然后用户可以对该文本文件进行插入或删除。

到目前为止,我在尝试编译时遇到了这个错误

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

用一些修复程序编译您的代码后,我确定问题出在私有 node 结构上。说明一切的编译器错误:

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**'

正如我之前在评论中所述,nodeHashTableSeparateChaining class 的私有本地结构,因此您不能在此 class 之外使用它就像在第三个片段中一样。通过将它移到外面或制作 public 并通过 HashTableSeparateChaining::node 引用来开始您的修复。它将解决这个问题。但还有更多。 fillHashTable 不通过引用获取参数并在循环内创建局部 hashtable 变量。恕我直言,该代码应如下所示:

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();}

类:

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 );                          
};