编译类模板成员函数时出错

Error while compiling class template member function

本文关键字:函数 出错 成员 编译      更新时间:2023-10-16

>我正在使用一个通用的哈希表类,但是当我在我的主目录中声明字符串时,它会给出以下错误。我假设问题是由"typename"部分引起的,但是当我更改为"类"时,问题并没有解决。

我的主要:

HashTable<string> H;

我的标题:

#ifndef QUADRATIC_PROBING_H
#define QUADRATIC_PROBING_H
#include <vector>
#include <string>
using namespace std;
int nextPrime( int n );
// QuadraticProbing Hash table class
//
// CONSTRUCTION: an approximate initial size or default of 101
//
// ******************PUBLIC OPERATIONS*********************
// bool insert( x )       --> Insert x
// bool remove( x )       --> Remove x
// bool contains( x )     --> Return true if x is present
// void makeEmpty( )      --> Remove all items
// int hash( string str ) --> Global method to hash strings
template <typename HashedObj>
class HashTable
{
  public:
    explicit HashTable( int size = 101 ) : array( nextPrime( size ) )
      { makeEmpty( ); }
    bool contains( const HashedObj & x ) const
    {
        return isActive( findPos( x ) );
    }
    void makeEmpty( )
    {
        currentSize = 0;
        for( size_t i = 0; i < array.size( ); i++ )
            array[ i ].info = EMPTY;
    }
    bool insert( const HashedObj & x )
    {
            // Insert x as active
        size_t currentPos = findPos( x );
        if( isActive( currentPos ) )
            return false;
        array[ currentPos ] = HashEntry( x, ACTIVE );
            // Rehash; see Section 5.5
        if( ++currentSize > array.size( ) / 2 )
            rehash( );
        return true;
    }
    bool remove( const HashedObj & x )
    {
        int currentPos = findPos( x );
        if( !isActive( currentPos ) )
            return false;
        array[ currentPos ].info = DELETED;
        return true;
    }
    enum EntryType { ACTIVE, EMPTY, DELETED };
  private:
    struct HashEntry
    {
        HashedObj element;
        EntryType info;
        HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY )
          : element( e ), info( i ) { }
    };
    vector<HashEntry> array;
    int currentSize;
    bool isActive( int currentPos ) const
      { return array[ currentPos ].info == ACTIVE; }
    int findPos( const HashedObj & x ) const
    {
        int offset = 1;
        size_t currentPos = myhash( x );
          // Assuming table is half-empty, and table length is prime,
          // this loop terminates
        while( array[ currentPos ].info != EMPTY &&
                array[ currentPos ].element != x )
        {
            currentPos += offset;  // Compute ith probe
            offset += 2;
            if( currentPos >= array.size( ) )
                currentPos -= array.size( );
        }
        return currentPos;
    }
    void rehash( )
    {
        vector<HashEntry> oldArray = array;
            // Create new double-sized, empty table
        array.resize( nextPrime( 2 * oldArray.size( ) ) );
        for( size_t j = 0; j < array.size( ); j++ )
            array[ j ].info = EMPTY;
            // Copy table over
        currentSize = 0;
        for( size_t i = 0; i < oldArray.size( ); i++ )
            if( oldArray[ i ].info == ACTIVE )
                insert( oldArray[ i ].element );
    }
    int myhash( const HashedObj & x ) const
    {
        int hashVal = hash( x );
        hashVal %= array.size( );
        if( hashVal < 0 )
            hashVal += array.size( );
        return hashVal;
    }
};
int hash( const string & key );
int hash( int key );
#endif

我收到以下错误:

    d:userssuuserdocumentsvisual studio 2010projectscs300-hw3cs300-hw3quadraticprobing.h(50): warning C4018: '>' : signed/unsigned mismatch
1>          d:userssuuserdocumentsvisual studio 2010projectscs300-hw3cs300-hw3quadraticprobing.h(41) : while compiling class template member function 'bool HashTable<HashedObj>::insert(const HashedObj &)'
1>          with
1>          [
1>              HashedObj=std::string
1>          ]
1>          d:userssuuserdocumentsvisual studio 2010projectscs300-hw3cs300-hw3compressor.cpp(130) : see reference to class template instantiation 'HashTable<HashedObj>' being compiled
1>          with
1>          [
1>              HashedObj=std::string
1>          ]
1>d:userssuuserdocumentsvisual studio 2010projectscs300-hw3cs300-hw3quadraticprobing.h(120): error C2872: 'hash' : ambiguous symbol
1>          could be 'hash'
1>          or       'c:program filesmicrosoft visual studio 10.0vcincludexfunctional(763) : std::hash'
1>          d:userssuuserdocumentsvisual studio 2010projectscs300-hw3cs300-hw3quadraticprobing.h(119) : while compiling class template member function 'int HashTable<HashedObj>::myhash(const HashedObj &) const'
1>          with
1>          [
1>              HashedObj=std::string
1>          ]

错误告诉您hash(const std::string&)函数与具有相同参数的std::hash重载之间存在冲突。这是您应该避免的原因之一 using namespace std .

通常的建议:永远不要在标头中使用using namespace std;

我个人的建议:永远不要在任何地方使用using namespace std