在散列 Table 中使用派生 类 的搜索函数

Search Function Using Derived Classes in a Hash Table

我对派生的 classes 以及它们如何利用从其父 class 继承的搜索功能有一些疑问。

这是我的 .h 文件

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

#define TABLESIZE 13

#ifndef HASH_H
#define HASH_H

namespace HTGroup
{
    template<class T>
    class HashTable
    {
    protected:
        struct item {
            T x;
            item* next;
        };
        item* HT[TABLESIZE];
        virtual int hash(T key) = 0;
        virtual int collision(T key, int &value) = 0;
    public:
        HashTable();
        virtual void printGrid();
        void insert(T key);
        void remove(T key);
        void search(T key);
        int indexItems(int index);
    };

    template<class T>
    class DHT1 : public HashTable<T>
    {
    protected:
        int hash(T key);
        int collision(T key, int &value);
        struct item {
            T x;
            item* next;
        };
        item* HT[TABLESIZE];
    public:
        DHT1();
        void printGrid();
    };

    template<class T>
    class DHT2 : public HashTable<T>
    {
    protected:
        int hash(T key);
        int collision(T key, int &value);
        struct item {
            T x;
            item* next;
        };
        item* HT[TABLESIZE];
    public:
        DHT2();
        void printGrid();
    };
}

#endif

这是我为搜索功能实现的:

template<class T>
void HashTable<T>::search(T key)
{
    int index = hash(key);
    bool foundKey = false;
    string item;

    item* temp = HT[index];
    while(temp != NULL)
    {
        if(temp->x == key)
        {
            foundKey = true;
            item = temp->x;
        }
        temp = temp->next;
    }

    if(foundKey == true)
    {
        cout << "Item was found." << endl;
    }
    else
    {
        cout << "Item was not found." << endl;
    }
}

这就是我在 main 中调用函数的方式:

hashy1.search(item);

我从我的搜索实现中的这一行中收到编译器错误:

item* temp = HT[index];

给我这个错误:

[Error] 'temp' was not declared in this scope

根据我的理解,每当派生 class 的对象调用搜索函数时,它就会混淆创建的指针是父 class 还是派生 class.

但奇怪的是,它让我在删除函数中创建了其他指针,没有任何问题,而且工作正常:

template<class T>
void HashTable<T>::remove(T key)
{
    int index = hash(key);

    item* delPtr;   //Where I am allowed to create pointers with
    item* P1;       //no issues
    item* P2;

    if(HT[index]->x == "")
    {
        cout << key << " was not found in the hash table" << endl;
    }
    else if ( HT[index]->x == key && HT[index]->next == NULL)
    {
        HT[index]->x = "";

        cout << key << " was removed from the hash table" << endl;
    }
    else if(HT[index]->x == key)
    {
        delPtr = HT[index];
        HT[index] = HT[index]->next;
        delete delPtr;

        cout << key << " was removed from the hash table" << endl;
    }
    else
    {
        P1 = HT[index]->next;
        P2 = HT[index];

        while(P1 != NULL && P1->x != key)
        {
            P2 = P1;
            P1 = P1->next;
        }

        if(P1 == NULL)
        {
            cout << key << " was not found in the hash table" << endl;
        }
        else
        {
            delPtr = P1;
            P1 = P1->next;
            P2->next = P1;

            delete delPtr;
            cout << key << " was removed from the hash table" << endl;
        }
    }

}

我试过像这样在 .h 文件中创建指针:

template<class T>
    class DHT1 : public HashTable<T>
    {
    protected:
        int hash(T key);
        int collision(T key, int &value);
        struct item {
            T x;
            item* next;
            item* temp; // Added declaration
        };
        item* HT[TABLESIZE];
    public:
        DHT1();
        void printGrid();
    };

但这仍然给我申报问题

在实现我的搜索功能时是否应该使用不同的方法,例如函数调用中的任何额外参数?或者也许我只是没有正确理解逻辑?

感谢您的任何回复!

您将 item 声明为 std::string,然后在与类型相同的范围内使用 item

  string item;  // <-- declaring as string
  item* temp = HT[index];  // <-- Compiler doesn't know what to do with this line except to give an error.

最简单的解决方案是将 std::string 变量命名为 item 以外的其他名称。