简体   繁体   English

在哈希表单独链接中没有匹配函数可调用xxx

[英]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++. 目前,我正在尝试使用单独的链接算法将二进制搜索树转换为哈希表,以在DEV-C ++中开发一个简单程序。

So far, I've converted the insert,delete,search and display operation function, along with hash function. 到目前为止,我已经转换了插入,删除,搜索和显示操作功能以及哈希函数。

item list.txt: 项目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. 我卡住的部分是我不知道如何将 item list.txt的所有项目入哈希表。

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: 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.tx t and the data already being hashed. 因此,运行该程序后,我希望可以显示item.tx t 项目中的所有现有数据以及已被哈希处理的数据。 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&)' 没有匹配的函数来调用`HashTableSeparateChaining :: insert(node **,node **,Item&)'

After compiling your code with some fixes I'm sure that problem is private node struct. 用一些修复程序编译代码后,我确定问题出在私有node结构上。 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. 正如我之前在注释node已经说过的那样,它是HashTableSeparateChaining类的私有本地结构,因此您不应像第3个代码段那样在此类之外使用它。 Begin your fixes with moving it outside or make public and refer via HashTableSeparateChaining::node . 通过将其移出外部或公开进行修复,并通过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. fillHashTable不会通过引用获取参数,而是在循环内创建局部hashtable变量。 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 );                          
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM