【问题标题】:no matching function for call to xxx in Hash Table Separate Chaining在哈希表单独链接中调用 xxx 没有匹配的函数
【发布时间】:2015-11-24 08:28:41
【问题描述】:

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

到目前为止,我已经转换了插入、删除、搜索和显示操作函数,以及哈希函数。

项目列表.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的所有项目插入到哈希表中。

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

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

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

插入方法:

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 中的所有现有数据以及已经被散列的数据。然后用户可以对该文本文件进行插入或删除。

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

没有匹配的调用函数 `HashTableSeparateChaining::insert(node**, node**, Item&)'

【问题讨论】:

  • 在哪里你得到错误?这是编译器的唯一输出吗?
  • 什么是Item 类型?
  • @JoachimPileborg 在hashtable.insert(&amp;A[hv], &amp;A[hv], i); 处出错。程序的输出取决于用户选项。 eg:输入1表示插入,2表示删除,3表示显示
  • @Jerome Item 是一个具有 item_id、price 和 item_name 的对象
  • nodeHashTableSeparateChaining 类的私有本地结构,因此您不应该像在 3rd sn-p 中那样在此类之外使用它。将它移到外面开始你的修复或制作public并通过HashTableSeparateChaining::node引用

标签: c++ algorithm hashtable chaining


【解决方案1】:

通过一些修复编译您的代码后,我确定问题是私有的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**'

正如我之前在评论node 中所说的那样,是HashTableSeparateChaining 类的私有本地结构,所以你不应该像在第三个sn-p 中那样在这个类之外使用它。将其移至外部或公开并通过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 );                          
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2020-08-24
    • 2021-05-08
    • 2018-10-29
    • 2021-06-25
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多