【发布时间】:2019-04-22 17:12:41
【问题描述】:
如果 addToHashTable() 函数中的条件抛出异常:抛出异常:读取访问冲突。 _Right_data 为 0x8。发生了。 hashTable 是指针类型 getNode() 返回字符串。因此,当我调用 getNode 时,它会返回其中存在的单词。然后我想将此单词与空字符串进行比较,以检查节点是否为空,但在这一行抛出异常。任何人请帮助我如何克服这个异常
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
【问题讨论】:
标签: c++