【问题标题】:C++ Templated HashMap implementation errorsC++ 模板化 HashMap 实现错误
【发布时间】:2012-10-27 02:01:42
【问题描述】:

我的HashMap 函数似乎存在一些模板问题。最后我需要运行一个<string, vector<string>> 哈希映射。它带来了很多问题。

我需要能够在向量上推送项目。我使用的是 [] 运算符。

测试

using namespace std;
#include "MyHashMap.hpp"
#include <vector>
int main()
{
    MyHashMap <string, vector <string> > test (10);
    test["x"].push_back("Y");    
}

错误(有很多。我认为它们是相关的,所以我只会显示 1 个)

在成员函数'valueType& MyHashMap::operator [with keyType = std::basic_string, std::allocator >, valueType = std::vector, std::allocator >, std::allocator, std::allocator > > >]':

test.cpp:13: 从这里实例化

MyHashMap.hpp:52: 错误: 无法转换'MyHashMap, std::allocator >, std::vector, std::allocator >, std::allocator, std::allocator > > >::EntryType'分配给“EntryType” MyHashMap.hpp:在成员函数'int MyHashMap::findPos(keyType&) [with keyType = std::basic_string, std::allocator >, valueType = std::vector, std::allocator >, std::allocator, std ::分配器 >> >]':

MyHashMap.hpp:46: 从 'valueType& MyHashMap::operator 实例化 [with keyType = std::basic_string, std::allocator >, valueType = std::vector, std::allocator >, std::allocator, std::allocator > > >]'

MyHashMap.hpp

#include <string>
#include "HashEntry.hpp"
using namespace std;

template <typename keyType, typename valueType>
class MyHashMap
{
    public:
        /***********************************************
        explicit HashTable( int size = 1009 ) : array(size)
        CONSTRUCTOR with default size of 1009
        ***********************************************/

        explicit MyHashMap(int size=1000):tableSize(size)
        {
        array = new HashEntry <keyType, valueType> [tableSize];
        }

        int size()
        {
        return tableSize;
    }

    /***********************************************

    ***********************************************/
    valueType& operator[](keyType x )
    {
        // Insert x as active
        int currentPos = findPos( x );
        if( isActive( currentPos ) )
        {
            return array[currentPos].value;
        }
        array[ currentPos ].key = x;
        array[ currentPos ].info = ACTIVE;
        return array[currentPos].value;
    }
    /***********************************************
    Returns pointer to HashEntry if it is active
    Returns NULL otherwise
    ***********************************************/
    HashEntry<keyType,valueType>& getPos(int n)
    {
        if(isActive(n))
        {
            return array[n];
        }
    }

    /***********************************************
    bool insert(  keyType & x,  & valueType y)
    inserts x and y into hashMap    
    ***********************************************/
    bool insert(  keyType & x,  valueType&  y)
    {
        // Insert x as active
        //currentPos gets position to try to insert 
        int currentPos = findPos( x );
        if( isActive( currentPos ) )
        {
            return false;
        }
        array[ currentPos ].key = x;
        array[ currentPos ].value = y;
        return true;
    }


    /***********************************************
    LAZY DELETION
    ***********************************************/
    bool remove(  keyType x )
    {
        int currentPos = findPos( x );
        if( !isActive( currentPos ) )
            return false;
        array[ currentPos ].info = DELETED;
        return true;
    }

    /***********************************************

    ***********************************************/
    valueType* find(  keyType & x ) 
    {
        //currentPos gets position where Search for x terminates
        int currentPos = findPos(x);
        if(isActive(currentPos))
            return array[currentPos].value;
        else
            return NULL;

    }

    enum EntryType { ACTIVE, EMPTY, DELETED };

private:

    HashEntry <keyType, valueType>* array;
    int tableSize;
    /***********************************************
    ***********************************************/
    bool isActive( int currentPos ) 
    {
        return array[ currentPos ].info == ACTIVE; 
    }

    /***********************************************
    int findPos(  HashedObj & x ) 
    Returns the position where the search for x terminates
    ***********************************************/
    int findPos(  keyType & x ) 
    {
        int offset = 1;
        int 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 >= tableSize )
                currentPos -= tableSize;
        }

        return currentPos;
    }
    /***********************************************
    Hash function for string
    ***********************************************/

    int myhash(  string & x )
    {
        int hashVal = 0;
        for(int i = 0; i < x.length(); i++)
        {
            hashVal = 37*hashVal+x[i];
        }
        hashVal %= tableSize;
        if(hashVal < 0)
            hashVal += tableSize;
        return hashVal;
    }
};



#endif

哈希条目

using namespace std;

enum EntryType { ACTIVE, EMPTY, DELETED };
template <typename keyType, typename valueType>

struct HashEntry
{
    keyType key;
    valueType value;
    EntryType info;
    /***********************************************
    HashEntry CONSTRUCTOR
    ***********************************************/
    HashEntry(keyType x = keyType(), valueType y = valueType(),
     EntryType i = EMPTY ):key(x), value(y),info( i ) { }
};
#endif

【问题讨论】:

    标签: c++ compiler-errors hashmap implementation


    【解决方案1】:

    您的会员MyHashMap::array的声明有误。将其更改为:

    HashEntry &lt;keyType, valueType&gt;* array;

    在构造函数中:

    explicit MyHashMap(int size = 1000) : tableSize(size)
    {
        array = new HashEntry<keyType, valueType>[tableSize];
    }
    

    它应该可以正常工作!

    【讨论】:

    • 其实[0u]前面多了两个**。抱歉,我没有注意到。
    • 试试这个。我认为错误是在 array 的声明前使用了 []。
    • 谢谢!!!现在我只需要弄清楚我的其他错误。无论如何你可以帮助我的操作员[]?
    • @user1645240 很好,现在您可以接受答案了 :) 我认为这里不适合讨论,但是是的,我可以帮助您!
    猜你喜欢
    • 2014-04-20
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多