没有模板参数列表,不能引用类模板

时间:2012-10-21 00:35:37

标签: c++ xcode class templates arguments

我是C ++的新手。这是我的作业,下面是教授给我们的代码,以帮助我们完成这项任务,但它没有编译......我已经标记了产生错误的行,错误信息是 “没有模板参数列表,不能引用模板'hash'。” 我不确定如何解决它。有人能指出我正确的方向吗? (我已经删除了那些与错误消息无关的行。)

该类定义为:

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

1 个答案:

答案 0 :(得分:3)

我怀疑您是using namespace std;(我可以告诉您,因为您使用vector而没有std::)并且hash命名空间中存在名称std ,所以名字冲突。

您应该使用std::而不是将整个std命名空间,特别是放入头文件中,无辜的用户可以#include将其污染并污染他们的程序也是std