【发布时间】: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