使用哈希表中的派生类搜索函数

时间:2015-11-30 16:38:06

标签: c++ inheritance search hashtable derived-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

根据我的理解,只要派生类的对象调用搜索函数,就会对创建的指针是否属于父类或派生类感到困惑。

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

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

但这仍然给我声明问题

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

感谢您的回复!

1 个答案:

答案 0 :(得分:0)

您将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以外的其他内容。