引发异常:读取访问冲突。 ** _ Right_data **为0x8。发生

时间:2019-04-21 16:04:26

标签: c++

如果addToHashTable()函数中的条件引发异常:引发异常:读取访问冲突。 _Right_data 为0x8。发生。 hashTable是指针类型,getNode()返回字符串。因此,当我调用getNode时,它返回其中存在的单词。那么我想将此单词与空字符串进行比较以检查Node是否为空,但会抛出此行异常。任何人都可以帮助我如何克服此异常

static int const hashTableSize = 15;
Node *hashTable[hashTableSize];
.........................................................
void HashTable::addToHashTable(int hashIndex, string word)
{
    Node *node = new Node(word);
    if (hashTable[hashIndex]->getNode() == "")//exception throws at this 
    {``
        hashTable[hashIndex] = node;
    }
    else
    {
        if (hashTable[hashIndex]->getNode() != word)
        {
            node->link = hashTable[hashIndex];
            hashTable[hashIndex] = node;
        }
    }
    return;
}
.............................................
string Node::getNode()
{
    return this->word;
}
..........................................
class Node
{
public:
    Node *link;
    Node *pointNextWrongWord;
    Node();
    Node(string word);
    string getNode();
    ~Node();

private:
    string word;
    int priority;
};
................................
Main:
cout << "Type the word you want to add in Dictionary : ";
cin >> word;
hashIndex = hashTable.hashFunction(word);
hashTable.addToHashTable(hashIndex, word);

i expect this if condition compare the string returned by getNode() 
function with "" (empty string) rather than throwing exception

1 个答案:

答案 0 :(得分:0)

看您的代码,我的印象是哈希表没有分配内存。

这段代码不正确

if (hashTable[hashIndex]->getNode() == "")//exception throws at this 
{
    hashTable[hashIndex] = node;
}

可能您需要一个初始化路由,将字符串单词设置为“”

init()
   { 
      for (int i =0 ; i < sizeof(hashTable)/sizeof(hasTable[0]); ++i)
      {
          hashTable[hashIndex] = new Node("");

      }
   }
相关问题