不能在没有模板参数列表的情况下引用类模板

Cannot refer to class template without a template argument list

本文关键字:情况下 引用 列表 参数 不能      更新时间:2023-10-16

我是C++新手。这是我的家庭作业,下面是教授给我们的代码,以帮助我们完成这项作业,但它无法编译...... 我已经标记了生成错误的行,错误消息是 "没有模板参数列表,则无法引用模板'哈希'"。
我不知道如何解决它。 有人可以指出我正确的方向吗?
(我已经删除了我认为与错误消息无关的行。

该类定义为:

template <typename HashedObj>
class HashTable
{
public:
//.... 
private:
struct HashEntry
{
HashedObj element;
EntryType info;
HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY )
: element( e ), info( i ) { }
};
vector<HashEntry> array;
int currentSize;
//... some private member functions....
int myhash( const HashedObj & x ) const
{
int hashVal = hash( x ); <<--- line with error
hashVal %= array.size( );
if( hashVal < 0 )
hashVal += array.size( );
return hashVal;
}
};
int hash( const HashedObj & key );
int hash( int key );

--- 和 cpp 文件----中的 int hash() 函数

int hash( const string & key )
{
int hashVal = 0;
for( int i = 0; i < key.length( ); i++ )
hashVal = 37 * hashVal + key[ i ];
return hashVal;
}
int hash( int key )
{
return key;
}

我怀疑您using namespace std;(我可以判断,因为您使用的是没有std::vector),并且std命名空间中存在一个名称hash,因此名称冲突。

您应该使用std::而不是引入整个std命名空间,尤其是在头文件中,无辜用户可以#include它并用std污染他们的程序。