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 ;
	}
}

Recommended Answers

All 2 Replies

>current = new Node ; // creates a new node
This uses the default constructor.

>current = new Node ; // creates a new node
This uses the default constructor.

ok, thank you. So I fixed that but now the program runs but the values are being overridden....the linked list is not functioning as it should for allowing more than one value in the same index. How do I correct this?

changes made, everything else the same:

struct NodeType {
	int x;
	NodeType *next;
};

struct Node {
	/** member variables */
	int key ;
	string value ; 
	
	/** 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
   NodeType *current = NULL, *prev = NULL, *head = NULL ;

   for (int i=0; i < 8; i++) {      // iterates eight times
      current = new NodeType ;          // 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
   }

oh, in main I changed the keys also, they are now:

int keys[length]= {2, 10, 5, 7, 11, 12, 0};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.