Hello all,
Lots of code here, but much of it can be ignored. I am getting an error on line 34 of HashTable.h, stemming from line 50 of main.cpp. I am trying to create an array of lists in the HashTable.h file.
Error code is:
\HashTable.h(34) : error C2440: '=' : cannot convert from 'std::list<_Ty>' to 'int'
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(97); //should this be "key" not "int"?
hTable1.hash_function(5);
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(const Entry &x); //constructor
Key hash_function( const Entry &x) const;
void insert( const Entry &k);
std::vector<list<Key>> my_vec[97];
};
template <class Entry>
HashTable<Entry>::HashTable(const Entry &x)
{
int size = x;
int *ptr;
ptr = new int [size];
ptr[5] = 4;
cout << "ptr : " << ptr[5] << endl;
for(int i = 0; i < size; i++)
{
std::list<int> mylist;
ptr[i] = mylist;
}
}
template <class Entry>
Key HashTable<Entry>::hash_function( const Entry &x) const
{
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( const 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
utility.h
#ifndef UTILITY_H
#define UTILITY_H
//Gives ANSI version of standard includes
//Also defines enumerated type for error
//messages
#include <iostream>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <cstddef>
#include <fstream>
#include <cctype>
#include <ctime>
#include <string>
#include <iomanip>
#include <queue>
#include <sstream>
#include <list>
using namespace std;
enum Error_code {
success, fail, exceeds_range,
not_present, duplicate_error, underflow, overflow
};
#endif //UTILITY_H