首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hash表分离链中对xxx的调用没有匹配函数

Hash表分离链中对xxx的调用没有匹配函数
EN

Stack Overflow用户
提问于 2015-11-24 08:28:41
回答 1查看 426关注 0票数 0

目前,我正在尝试将二叉树转换为哈希表,使用单独的链式算法在DEV++中开发一个简单的程序。

到目前为止,我已经转换了insert、delete、search和display操作函数以及哈希函数。

项目list.txt:

代码语言:javascript
复制
    Item ID  Item Price Item Name
    10001     23.00     Book1 
    10002     24.00     Book2
    10003     31.98     Book3
    10004     41.90     Book4

我陷入困境的部分是,我不知道如何将所有的项list.txt项插入到哈希表中。

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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 );                          
};

插入方法:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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(节点**,节点**,Item&)‘的调用没有匹配函数

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-24 09:14:36

在用一些补丁编译代码之后,我确信这个问题是私有的node结构。编译器错误,它澄清了所有内容:

代码语言:javascript
复制
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类的私有本地结构,因此您不应该能够像在第3段那样在该类之外使用它。从将其移出外部开始您的修复,或者公开并通过HashTableSeparateChaining::node提供参考。它会解决这个问题。但还有更多。fillHashTable不引用参数,而是在循环中创建本地hashtable变量。该代码应该如下所示:

代码语言:javascript
复制
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();}

课程:

代码语言:javascript
复制
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 );                          
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33888709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档