指定行的段错误:哈希表插入/搜索函数

时间:2015-12-01 19:30:09

标签: c++ xcode hashtable

我收到了EXC_BAD_ACCESS错误。我试图将单词插入哈希表并使用单独的链接。这是我的班级Hash.h,其中有wordData个班级来存储单词,pageNumbers单词出现在:

class Hash
{

  private:
    class wordData
    {
      public:
        string word;
        vector < int >pageNum;
        wordData *nextWord;

        // Initializing the next pointer to null in the constructor
          wordData()
        {
            nextWord = nullptr;
        }

        // Constructor that accepts a word and pointer to next word
        wordData(string word, wordData * nextWord)
        {
            this->word = word;
            this->nextWord = nextWord;
        }

        // Getting and setting the next linked word
        wordData *getNext()
        {
            return nextWord; //-------------------> BAD_ACCESS ERROR
        }

        void setNext(wordData * newInfo)
        {
            nextWord = newInfo;
        }

        // Setting info for the word node.
        void setInfo(string & w, int pNum)
        {
            this->word = w;
            this->pageNum.push_back(pNum);
        }

        // ******************* Gives a thread-bad access error************************
        string getWord()
        {
            return word;
        }

        void addPageNums(int x)
        {
            this->pageNum.push_back(x);
        }
    };

  private:
    // Head to point to the head node of the linked list for a particular word
    wordData ** head;
    int size;
    int *bucketSize;
    int totalElements;

  public:

    // Class hash function functions
    Hash();
    // Function to calculate bucket number based on string passed
    int hashFunction(string key);

    // search if word is present
    bool Search(string);

    // Insert word
    void Insert(string, int);

    int bucketNumberOfElements(int index);
};

#endif /* Hash_h */

运行调试器后,我发现nextWord的值为0x00000000000,我理解它与nullptr不同,但是归因于NULL赋值,尽管我似乎无法弄清楚在哪里以及为什么。我还没有包含Hash.cpp文件,因为我认为有一个明显的指针操作,我在.h文件中做错了。

任何帮助将不胜感激。感谢。

0 个答案:

没有答案