0
votes

I written a simple program for insert data in Hash Table and remove data from it with C++ language. My program error is: undefined reference to 'HashTable::insert(int&)' How to fix this error?

#include <iostream>
#include <list>
#include <vector>

using namespace std;

I Create a template for give all variables.

template <typename HashedObj>
class HashTable {
public:
    explicit HashTable(int cusize = 101) {}

    bool contains(const HashedObj& x) const
    {
        auto& whichList = theLists[myhash(x)];
        return find(begin(whichList), end(whichList), x) != end(whichList);
    }

    void makeEmpty()
    {
        for (auto& thisList : theLists)
            thisList.clear();
    }

    bool insert(const HashedObj& x)
    {
        auto& whichList = theLists[myhash(x)];
        if (find(begin(whichList), end(whichList), x) != end(whichList))
            return false;
        whichList.push_back(x);

        //Rehash
        if (++currentSize > theLists.size())
            rehash();

        return true;
    }

    bool insert(HashedObj& x);
    bool remove(const HashedObj& x)
    {
        auto& whichList = theLists[myhash(x)];
        auto itr = find(begin(whichList), end(whichList), x);

        if (itr == end(whichList))
            return false;

        whichList.erase(itr);
        --currentSize;
        return true;
    }


private:
    vector<list<HashedObj>> theLists; //The array of Lists
    int currentSize;

    void rehash();
    size_t myhash(const HashedObj& x) const
    {
        static hash<HashedObj> hf;
        return hf(x) % theLists.size();
    }
};

In the main function i create a HashTable with int variable for example and insert 10 number to it but compiler has a error on test = t.insert(i); for insert function.

int main()
{
    HashTable<int> t;

    bool test;
    for (int i = 0; i < 10; i++) {
        test = t.insert(i);
    }
}
2
tip: look at the paramenter of <br> bool insert(const HashedObj& x) - travisjayday
The easiest fix is to remove this declaration bool insert(HashedObj& x);. Alternatively, you may want to implement this overload. Finally, you can use t.insert(std::move(i)) instead of t.insert(i) (you probably don't want to do that with non-trivial types, though). The options are listed in the order of my preference. - Dietmar Kühl
also, you need to #include <algorithm> - Doug Coburn
Then you'll have a problem with rehash being declared but not defined. - Doug Coburn
Is the second version of insert defined in a separate source file? If so, what's its definition? If not, why have you declared it? - Sam Marinelli

2 Answers

2
votes

There seems to be two bool insert(params) functions in your code:

 bool insert(const HashedObj& x)
{
    auto& whichList = theLists[myhash(x)];
    if (find(begin(whichList), end(whichList), x) != end(whichList))
        return false;
    whichList.push_back(x);

    //Rehash
    if (++currentSize > theLists.size())
        rehash();

    return true;
}

and here:

bool insert(HashedObj& x);

Remove one of them.

Edit: As @DietmarKühl suggested, you should remove the second implementation.

0
votes

You declared two insert functions, the first one took a const HashedObj& parameter, the second one took a HashedObj& parameter.

test = t.insert(i); i is a non-const variable here, so it matches the second insert function, but you didn't implement it.

It could work well if you remove the second insert declaration, because the compiler will convert i to a const int& type automaticlly.