Thread: Hash table help

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Hash table help

    Hi, im getting these errors for my code, can someone help please.

    hash.cpp: In function 'int main()':
    hash.cpp:50: error: invalid conversion from 'int' to 'data*'
    hash.cpp:51: error: invalid conversion from 'data*' to 'int'
    hash.cpp:51: error: initializing argument 1 of 'data::data(int)'
    linhash.h: In member function 'int linHashtable<dataType>::insert(const dataType&) [with dataType = data]':
    hash.cpp:51: instantiated from here
    linhash.h:60: error: passing 'const data' as 'this' argument of 'int data::getHashval(int)' discards qualifiers


    Code:
    #include <iostream>
    #include "linhash.h"
    
    /* Small program to explore using hash tables.
    
       1. The hash table takes up to 400 items
       2. The program generates 400 random six digit numbers.
       3. It uses a hashing routine to place them in the hash table.
       4. The program prints a report on the percantage of clashes that 
          occur for every 20 numbers placed in the table.
    */ 
    
    using namespace std;
    
    struct data
    {
       int x;
       
       data() : x(-1) {
       }
       
       data(int n) : x(n) {
       }
       
       bool empty() {
          return (x < 0);
       }
       
       int getHashval(int range) {
          return (x % range);
       }
    };
    
    int main()
    {
       const int SIZE = 400;
       int count, totalClashes=0;
       int clashes = 0, numClashes;
       data *dataPtr;
       
       // declare linera hash table
       linHashtable<data> hashTable(SIZE);
    
       // initialise the random number generator
       srand(1);
    
       // generate 400 six digit numbers and put in hash table
       for (count=1; count<=SIZE; count++) {
          // create 6 digit number and then insert in hash table
          dataPtr = new data(rand() % 1000000);    
          numClashes = hashTable.insert(dataPtr);
          
          // build up hashing data
          if (numClashes > 0) {
             totalClashes += numClashes;
             clashes++;
          }
             
          // print a report with each set of 20 numbers added to table
          if ((count+1) % 20 == 0)
          {
             cout << "% full = " << (count+1) * 100 / SIZE << "\n";
             cout << "% clashing = " << clashes * 100 / (count+1) << "\n";
             cout << "ave search time = " << (count+1 + totalClashes) / (count+1) << "\n\n";
          }
       }
    
       return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. You can either do new or you can use an initializer, none of this "both at the same time" sort of thing. According to the error message, hashTable.insert takes a data, not a data*, so there is no reason or excuse for having a pointer to data in the first place.

    2. Since hashTable.insert treats its data as const, all the class methods required by insert must be const as well; specifically, getHashVal must be a const function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hash Table
    By mrsirpoopsalot in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2009, 09:10 PM
  2. hash table in C
    By -EquinoX- in forum C Programming
    Replies: 7
    Last Post: 03-25-2008, 09:16 PM
  3. Hash Table
    By Cpro in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 02:14 PM
  4. What is a Hash Table?
    By rip1968 in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2002, 08:57 PM
  5. Hash table
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-14-2002, 07:06 PM