指针对象未在范围内声明

Pointer object not being declared in scope

这让我发疯。在搜索函数定义中,Dev c++ 表示

item* temp = HT[index];

声明超出范围。为什么会这样?我这辈子都弄不明白。这行代码在其他函数中有变体,编译器对它们没有问题。非常感谢任何帮助,谢谢。

编辑:对不起大家,我的错。我删除了很多不必要的代码以使其更易于阅读。如果您想知道为什么缺少某个功能或某些东西,那是因为我删除了它。我在遇到问题的代码中添加了一些注释。

//-----hash.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


//---hash.cpp--------------------------------------------------
#include <iostream>
#include <string>
#include <cstdlib>
#include "hash.h"
#define TABLESIZE 1
using namespace std;

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

        item* delPtr;
        item* P1;
        item* P2;

        //I deleted the rest of this definition to save space.
        //The reason I kept the above code was to show that 
        //'item*' was being used in this function without any problem. 
        //In search, however, it says 'item*' is out of scope.         
    }

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

        //Deleted a few if statements from this one. The code above 
        //is causing the trouble. Specifically, "item* temp = HT[index]".
        //The compiler says it is declared out of scope. 

    }

//----main.cpp--------------------------------------------------

#include <iostream>
#include <string>
#include <cstdlib>
#include "hash.cpp"
//using namespace std;
using namespace HTGroup;

#define TABLESIZE 13

int main(int argc, char** argv) {

    return 0;
}

编译器在解析类型 item 时遇到问题,因为它与本地字符串变量也称为 item(在上面声明)冲突。

您可以为该字符串使用不同的名称,或者您可以完全限定 item 类型的名称,如下所示:

HashTable<T>::item * temp = HT[index];

它可能会帮助您使用强制您使用不与变量名冲突的类型名称的命名约定。 C++ 中的一个常见约定是类型名称以大写字母开头。