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);
}
}
bool insert(HashedObj& x);. Alternatively, you may want to implement this overload. Finally, you can uset.insert(std::move(i))instead oft.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#include <algorithm>- Doug Coburninsertdefined in a separate source file? If so, what's its definition? If not, why have you declared it? - Sam Marinelli