源代码如下:
// hash.cpp : Defines the entry point for the console application.
//
#pragma warning(disable:4786)
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <list>
#include <set>
#include <map>
using namespace std;
class HASH_F;
void hf(vector<string>& str, string*HT);
class Hash_F
{
public:
unsigned operator()( const string& k)const
{
unsigned int total = 0, n = k.size();
for(int i = 0; i < n; i++)
total += (i+1)*k[i];
return total;
}
};
void hf(vector<string>&vec, string *HT )
{
Hash_F hobj;
int prime = 31;
int n = vec.size();
for( int i = 0; i<n; i++ )
{
unsigned index = hobj(vec[i])%prime;
HT[index] = vec[i];
}
}template<typename Key, typename T>
struct value_type
{
public:
value_type(){}
value_type(const value_type& p):first(p.fisrt )
{
second = p.second;
}
value_type(const Key&k, const T&t):first(k)
{
second = t;
}
bool operator == ( const value_type& x )
{
return first == x.first;
}
const Key first;
T second;
};……
……template< typename Key, typename T, typename Hash_F>
class HashTable
{
public:
class iterator
{
friend class HashTable<Key,T,Hash_F>;
public:
iterator(){}
bool operator != (const iterator& otheriter ) const
{
//(ht != otheriter) ||
return ( currBucket != otheriter.currBucket) ||
( currPos != otheriter.currPos);
} iterator & operator = (const iterator& otheriter )
{
ht = otheriter.ht;
currBucket = otheriter.currBucket;
currPos = otheriter.currPos;
return *this;
}
value_type<const Key,T>& operator *() const
{
return *currPos;
}
iterator& operator ++()
{
if(++currPos != ht->HT[currBucket].end() )
return *this;
unsigned tempBucket = currBucket; while( tempBucket < ht->m-1 )
{
tempBucket++;
if( !ht->HT[tempBucket].empty() )
{
currBucket = tempBucket;
currPos = ht->HT[currBucket].begin();
return *this;
}
}
return *this;
}
private:
HashTable<Key,T,Hash_F>*ht;
int currBucket;
typedef typename value_type<const Key, T> val_type;
list<val_type>::iterator currPos;
iterator(HashTable<Key,T,Hash_F>*ptr,list<val_type>::iterator pos,int cb = 0):(ptr),currBucket(cb),currPos( pos ){}
};
friend class iterator;
HashTable( int nb = 0, Hash_F&hf = Hash_F() );
~HashTable(){ delete [] HT;};
bool empty() const { return count == 0;}
int size() const{ return count; }
iterator find(const Key& k);
T& operator[](const Key&k );
pair<iterator,bool>insert(const value_type<const Key,T>&item );
void erase( iterator iter );
iterator begin();
iterator end();
private:
int count;
int m;
typedef typename value_type<const Key, T> val_type;
list<val_type> * HT;
Hash_F hf;
};template<typename Key,typename T, typename Hash_F>
HashTable<Key,T,Hash_F>::iterator HashTable<Key,T,Hash_F>::begin()
{
HashTable<Key,T,HashTable>::iterator iter;
iter.ht = this;//错误出现在此处
int i = 0;
iter.currBucket = i;
iter.currPos = HT[i].begin();
for( ;i<m && HT[i].empty(); i++ );
if( i<m )
{
iter.currBucket = i;
iter.currPos = HT[i].begin();
}
return iter;//此处出现错误
}
vc6下编译出现如下错误
C:\Program Files\Microsoft Visual Studio\MyProjects\hash\hash\hasha.cpp(188) : error C2440: 'type cast' : cannot convert from 'class HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,class Hash_F> 
*const ' to 'class HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,class HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,class Hash_F> >'
        No constructor could take the source type, or constructor overload resolution was ambiguous