【问题标题】:Pointer object not being declared in scope指针对象未在范围内声明
【发布时间】:2016-03-04 21:41:24
【问题描述】:

这快把我逼疯了。在搜索函数定义中,Dev c++ 是说

item* temp = HT[index];

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

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

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

【问题讨论】:

  • 大量不相关的代码要涉足...
  • −1 大量无关代码。
  • 对不起大家,我不应该发布这么多代码。我的错。我删除了很多,希望足以让它可读。

标签: c++ pointers hashtable declaration


【解决方案1】:

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

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

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

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

【讨论】:

  • 啊,原来如此!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多