Nate Nate - 1 month ago 5
C++ Question

Pointer object not being declared in scope

This is driving me crazy. In the search function definition, Dev c++ is saying that

item* temp = HT[index];


is declared out of scope. Why is this happening? I can't for the life of me figure it out. There are variations of this line of code in other functions and the compiler has no problem with them. I'd really appreciate any help, thank you.

EDIT: Sorry everyone, my bad. I removed a lot of unnecessary code to make it easier to read. If you're wondering why a function or something is missing, it's because I deleted it. I added some comments in the code where I'm having the problem.

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

Answer

The compiler is having trouble resolving the type item because it conflicts with a local string variable also called item (which is declared just above).

You can either use a different name for that string, or you can fully-qualify the name for the item type as follows:

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

It might help you to use a naming convention that forces you to use type names that do not conflict with variable names. A common convention in C++ is for type names to begin with a capital letter.

Comments