|
-
May 3rd, 2008, 01:46 AM
#1
[RESOLVED] Inheritance with Templates: "Does not name a type
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
Code:
#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.
-
May 3rd, 2008, 01:59 AM
#2
Re: Inheritance with Templates: "Does not name a type
I tried compiling this code with VC++ 6.0
It compiles successfully.
Bharani
Rate the posts which you find useful
-
May 3rd, 2008, 02:46 AM
#3
Re: Inheritance with Templates: "Does not name a type
Do as the compiler says.. you need the 'typename' keyword with ElemPtr as noted in the error.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 3rd, 2008, 03:14 AM
#4
Re: Inheritance with Templates: "Does not name a type
 Originally Posted by ajbharani
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 3rd, 2008, 11:08 PM
#5
Re: Inheritance with Templates: "Does not name a type
 Originally Posted by exterminator
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.
-
May 3rd, 2008, 11:46 PM
#6
Re: Inheritance with Templates: "Does not name a type
 Originally Posted by ajbharani
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
-
May 4th, 2008, 11:35 PM
#7
Re: Inheritance with Templates: "Does not name a type
Okie. Thanks
Rate the posts which you find useful
-
May 5th, 2008, 09:30 AM
#8
Re: Inheritance with Templates: "Does not name a type
 Originally Posted by ajbharani
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 5th, 2008, 10:52 AM
#9
Re: Inheritance with Templates: "Does not name a type
Yes. It is
Rate the posts which you find useful
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|