This is a lot of code, but most can be ignored. I am just having a problem with using templates/types.
main.cpp(48) : error C2664: 'HashTable<Entry>::insert' : cannot convert parameter 1 from 'int' to 'int &'
There is basically a type mismatch between line 48 of main (int) and the HashTable::insert function.
What am I doing wrong?
main.cpp
#include "utility.h"
#include "Key.h"
#include "HashTable.h"
using namespace std;
int main()
{
const int num_files = 2;
const string fileNames[num_files] = {"Lab8a.txt", "Lab8b.txt"};
int temp_array300[300];
int temp_array750[750];
// Fill up temporary arrays
for (int j = 0; j < num_files; j++)
{
ifstream fin(fileNames[j].c_str());
if (!fin.is_open())
{
cout << endl << "Unable to open input file" << fileNames[j] << endl;
return 1;
}
int i = 0;
if (j == 0)
{
for (i = 0; i < 300; i++)
{
fin >> temp_array300[i];
//cout << i << ": " << temp_array300[i] << endl;
}
}
else
{
for (i = 0; i < 750; i++)
{
fin >> temp_array750[i];
//cout << i << ": " << temp_array750[i] << endl;
}
}
}
HashTable<int> hTable1;
hTable1.insert(3241);
cout << "hello world" << endl;
}
HashTable.h
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include "utility.h"
#include "Key.h"
#include <list>
using namespace std;
template <class Entry>
class HashTable
{
public:
HashTable(); //constructor
Key hash_function(Entry &x);
void insert(Entry &k);
std::vector<list<Key>> my_vec[97];
};
template <class Entry>
HashTable<Entry>::HashTable()
{
}
template <class Entry>
Key HashTable<Entry>::hash_function(Entry &x)
{
int remainder[4];
int data = x;
int hashed_data = 0;
cout << "Number is " << data << endl;
for(int i = 0; i < 4; i++)
{
remainder[i] = data % 10;
data = data/10;
cout << "Remainder: " << remainder[i] << endl;
cout << "x: " << data << endl;
}
hashed_data = remainder[3]*10 + remainder[2] + remainder[1]*1000 + remainder[0]*100;
cout << "Hash is " << hashed_data % 97 << endl;
return (hashed_data % 97);
}
template <class Entry>
void HashTable<Entry>::insert(Entry &k)
{
int index = hash_function(k);
my_vec[index] = k;
cout << "my_vec(index) = " << my_vec[index] << endl;
}
#endif //HASHTABLE_H
Key.cpp
#include "key.h"
double Key::comparisons = 0;
Key::Key(int v )
{
//ctor
key = v;
}
int Key::the_key() const
{
return key;
}
// Operator overload for == to compare the integer key
// member variables between two Key objects, also increments comparisons
bool operator ==(const Key &x, const Key &y)
{
++Key::comparisons;
return x.the_key() == y.the_key();
}
// Operator overload for > to compare the integer key
// member variables between two Key objects, also increments comparisons
bool operator>(const Key &x, const Key &y)
{
++Key::comparisons;
return x.the_key() > y.the_key();
}
// Operator overload for < to compare the integer key
// member variables between two Key objects, also increments comparisons
bool operator<(const Key &x, const Key &y)
{
++Key::comparisons;
return x.the_key() < y.the_key();
}
// Operator overload for >= to compare the integer key
// member variables between two Key objects, also increments comparisons
bool operator >=(const Key &x, const Key &y)
{
++Key::comparisons;
return x.the_key() >= y.the_key();
}
// Operator overload for <= to compare the integer key
// member variables between two Key objects, also increments comparisons
bool operator <=(const Key &x, const Key &y)
{
++Key::comparisons;
return x.the_key() <= y.the_key();
}
// Operator overload for != to compare the integer key
// member variables between two Key objects, also increments comparisons
bool operator !=(const Key &x, const Key &y)
{
++Key::comparisons;
return x.the_key() != y.the_key();
}
Key.h
#ifndef KEY_H
#define KEY_H
//header file for class Key. Keys are
//just integers with overloaded comparison
//operators to count compares.
class Key
{
public:
//static variable to count the number of
//times a comparison is made against a Key object
static double comparisons;
Key (int x = 0);
//constructor
//default is to set the integer value key to 0
int the_key() const;
//accessor function - to inspect key value
private:
int key;
};
//overload relational operators for type Key
//each also increments the comparisons counter variable
bool operator ==(const Key &x, const Key &y);
bool operator > (const Key &x, const Key &y);
bool operator < (const Key &x, const Key &y);
bool operator >=(const Key &x, const Key &y);
bool operator <=(const Key &x, const Key &y);
bool operator !=(const Key &x, const Key &y);
#endif //KEY_H