Im making a separate chaining hash table for dictionary words. In my insert method i use find to make sure there isnt a duplicate. Every thing compiles but my program stops at the find statement and quits working. If i omit the find method, which ok for the first word because there is no duplicate as of yet, it stops on the push back method for some weird reason. If someone could point out to me why it does this it would be greatly appreciated. I can post more of the code if needed.

Note:I also am not sure if my constructor is wrong or not.

//insert
bool DictHash::insert(const string &x)
{
    list<string> &newList = wordList[myHash(x)];     
    if(find(newList.begin(), newList.end(), x)!= newList.end())
    {        
        return false;
    }    
    newList.push_back(x);    
    if(++currentSize > wordList.size())
        rehash();
    return true;
}


//constructor
DictHash::DictHash(int size)
{
    vector<list<string> > wordList[size];
}

//main

using namespace std;

int main()
{
    ifstream infile("file name");
    DictHash *dictionary;
    dictionary = new DictHash(102001);
    if(infile.is_open())
    {
        while(infile.good())
        {
            string word;
            getline(infile, word);
            cout<<word<<endl;
            dictionary->insert(word);
        }
        infile.close();
    }
    return 0;
}

Recommended Answers

All 2 Replies

Your constructor declares a local array of size "size" called wordList. It will not initialize the data member of DictHash called wordList. I would assume that you use std::map for the type of wordList. In that case, it should work without initialization. Otherwise, you need to use an std::vector for wordList, and initialize it as follows:

//constructor
DictHash::DictHash(int size) : wordList(size) 
{
    
}

What is after the : is called the initialization list, and it is basically where you call all the constructors of all (or some of) the data members of the class, with whatever parameter they need.

dude you are awesome. thanks so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.