help! I am getting an error message, no appropriate default constructor available. I am not sure what I need the default constructor for? how do I correct this?
#include <iostream>
#include <string>
using namespace std;
struct Node {
/** member variables */
int key ;
string value ;
int x;
Node *next; //member pointer
/** constructor initializes */
Node(int key, string value) {
this->key = key ;
this->value = value ;
}
} ;
/** size of the array */
const int size = 10 ;
/** An implementation of a simple hash table */
class HashTable {
Node *nodeArray[size] ;
public:
/** this hash function returns an index to the array */
int hashFunction(int key) {
return (key % size) ;
}
/** put the value into the array using an index generated using hash function */
void put(int key, string value) {
int index = hashFunction(key) ;
nodeArray[index] = new Node(key, value) ;
// creates pointers that will be used to build the linked list
Node *current = NULL, *prev = NULL, *head = NULL ;
for (int i=0; i < 8; i++) { // iterates eight times
current = new Node ; // creates a new node
current->next = NULL ; // let next point nowhere
current->x = i ; // assign value to member variable
if (head == NULL) { // if this is the first node
head = current ; // let head point there
} else { // if this is not the first node
prev->next = current ; // let prev node point to this one
}
prev = current ; // let prev becomes the current node
}
}
/** returns the value that is matching the key */
string get(int key) {
int index = hashFunction(key) ;
if (nodeArray[index] != NULL)
return nodeArray[index]->value ;
return "" ;
}
} ;
/** An entry point for program execution */
int main() {
HashTable hashTable ;
const int length = 7 ;
int keys[length]= {1, 15, 23, 105, 500, 44, 29};
string values[length] = {"Earth", "Mars", "Venus", "Saturn", "Pluto", "Mercury", "Jupiter"} ;
// putting incoming data into the hash table
for (int i=0; i < length; i++) {
hashTable.put(keys[i], values[i]) ;
}
// the incoming data
cout << "The Incoming Data\n" ;
cout << "Keys \t Values" << endl ;
cout << "-----------------------------------------------------\n" ;
for (int i=0; i < length; i++) {
cout << keys[i] << '\t' << values[i] << endl ;
}
// the hash table result
cout << "\n\nThe hash table result\n" ;
cout << "-----------------------------------------------------\n" ;
for (int i=0; i < length; i++) {
cout << "hashTable(key) = " << "keys[i] = " << keys[i] << ", " << hashTable.get( keys[i] ) << endl ;
}
}