Click to See Complete Forum and Search --> : [RESOLVED] Inheritance with Templates: "Does not name a type


s.hanson
May 3rd, 2008, 01:46 AM
This is a snippet from a hash table program I am writing. It uses inheritance, where HashTable is abstract base, OpenAddressing is abstract and LinearProbeHash inherits from OpenAddressing


#include <vector>
#include <string>
using namespace std;

enum Status { EMPTY, USED, FREE };

///////////////////////////////////////////////////////////////
// HASH TABLE CLASS - ABSTRACT BASE //
///////////////////////////////////////////////////////////////
template <typename Object>
class HashTable
{
public:
struct HashElement// Storage "Bucket" for a hash element
{
int key;
Object el;
Status status;
HashElement(const int& k=0, const Object& e=NULL, Status st=EMPTY)
: key(k),el(e), status(st) {}
};
typedef HashElement* ElemPtr;

protected:
virtual ElemPtr finder(const int& k) = 0;
virtual ElemPtr inserter(const int& k, const Object& e) = 0;
virtual int hash1(const int& k) { return (k % N); }
int sz, N;
public:
virtual void removeElement(int k) = 0;
virtual ElemPtr find(int k) { return finder(k); }

};
///////////////////////////////////////////////////////////////
//OPEN-ADDRESSING CLASS - ABSTRACT BASE //
///////////////////////////////////////////////////////////////
template <typename Object>
class OpenAddressing : public HashTable<Object>
{
public:
void removeElement(int k)
{
ElemPtr temp = finder(k);
*temp = this->HashElement(FREE);
this->sz--;
}
protected:
ElemPtr HashArray;
virtual int hash2(const int& k, const int& i) = 0; // hash function
ElemPtr inserter(const int& k, const Object& e)
{
int i = 0;
int insertLoc = this->hash1(k); //starts insert at hash value of key
int startPoint = insertLoc; //keeps track of initial insert location
while (HashArray[insertLoc].status == USED)
{
this->collisions++; //keep track of collisions for testing
i++;
insertLoc = hash2(k,i);
if ( insertLoc == startPoint) //loop has gone all the way around the table
{
cerr << "Table Full!\n";
return NULL;
}
}
HashArray[insertLoc] = HashElement(k,e,USED);
this->sz++;
return &HashArray[insertLoc];
}
ElemPtr finder(const int& k)
{
int loc = this->hash1(k);
int startPoint = loc;
bool found = true;
int i=0;
while (HashArray[loc].key != k)
{
i++;
this->collisions++;
loc = hash2(k,i);
if (HashArray[loc].status == EMPTY || startPoint == loc)
{
found = false;
break;
}
}
if (found == false)
{
cerr << "Couldn't find element with key: " << k << endl;
return NULL;
}
else
// cout << "\nIterations to find " << k << "\t << i << endl;
return &HashArray[loc];
}
};
///////////////////////////////////////////////////////////////
// LINEAR PROBING CLASS - ABSTRACT BASE //
///////////////////////////////////////////////////////////////
template <typename Object>
class LinearProbeHash : public OpenAddressing<Object>
{
public:
LinearProbeHash(int capacity=107)
{
this->sz = 0;
this->N = capacity;
this->collisions = 0;
this->HashArray = new HashElement[this->N];
}
protected:
virtual int hash2(const int& k, const int& i)
{
return ((this->hash1(k)+i) % this->N);
}
};


I cannot figure this out. I get this error at several places:
Hash.h:55: error: ‘ElemPtr’ does not name a type
Hash.h:55: note: (perhaps ‘typename HashTable<Object>::ElemPtr’ was intended)

Does anyone know what I might be doing wrong. I have never done any serious implementation of templates with inheritance.

ajbharani
May 3rd, 2008, 01:59 AM
I tried compiling this code with VC++ 6.0
It compiles successfully.

Bharani

exterminator
May 3rd, 2008, 02:46 AM
Do as the compiler says.. you need the 'typename' keyword with ElemPtr as noted in the error.

exterminator
May 3rd, 2008, 03:14 AM
I tried compiling this code with VC++ 6.0
It compiles successfully.Aah. Still with VC++ 6.0? Look out for the express editions for 2005/2008 versions. They are freely downloadable.

s.hanson
May 3rd, 2008, 11:08 PM
Do as the compiler says.. you need the 'typename' keyword with ElemPtr as noted in the error.

Thanks. I don't know why I hadn't tried that.

Paul McKenzie
May 3rd, 2008, 11:46 PM
I tried compiling this code with VC++ 6.0
It compiles successfully.VC++ 6.0 is not correct, since it is not a conforming ANSI C++ compiler. The code that was posted is wrong.

Never use Visual C++ 6.0 to determine if templated code is correct or not.

Regards,

Paul McKenzie

ajbharani
May 4th, 2008, 11:35 PM
Okie. Thanks :)

exterminator
May 5th, 2008, 09:30 AM
Hope is a good thing; may be the best of all; and no good thing ever dies.Isn't that from The Shawshank Redemption... I love that movie. :)

ajbharani
May 5th, 2008, 10:52 AM
Yes. It is :)