Thread: Data Type Errors

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Data Type Errors

    Can anyone help with the below compiling errors please?

    hash.cpp: In function 'int main()':
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Which line is 51?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> linHashtable<data>

    It looks like whatever linHashtable is, it is holding data objects, but you're trying to pass it a pointer. Perhaps instead of this:
    Code:
    dataPtr = new data(rand() % 1000000);
    use this:
    Code:
    data dataObj(rand() % 1000000);
    and just use dataObj instead of the pointer.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    30
    I think we are at the same subject. I had the same one.
    I did try the same as Daved but same error still there.

    in linhash.h file, there is
    int insert(const dataType& newData)

    data datatPtr now is an dataType as struct data has defined.

    Code:
    data dataPtr = (rand() % 1000000);
    numClashes = hashTable.insert(dataPtr);
    In member function ‘int linHashtable<dataType>::insert(const dataType&) [with dataType = data]’:
    hash.cpp:53: instantiated from here
    linhash.h:60: error: passing ‘const data’ as ‘this’ argument of ‘int data::getHashval(int)’ discards qualifiers
    Last edited by cubimongoloid; 05-11-2009 at 10:21 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    1
    I got the code to work as below but I can't get it to work logically as it is supposed to. Does anyone know how to do it?


    Code:
    #include <iostream>
    #include <algorithm>
    #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);
       }
    
       data(const data &other) : x(other.x)
       { cout << "hi";
       }
       
       int getHashval(int range) const {
          return (x % range);
       }
    };
    
    int main()
    {
       const int SIZE = 400;
       int count, totalClashes=0;
       int clashes = 0, numClashes = 0;
       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;) {
          // 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) % 20 == 0)
          {
             cout << "% full = " << (count) * 100 / SIZE << "\n";
             cout << "% clashing = " << clashes * 100 / (count) << "\n";
             cout << "ave search time = " << (count + totalClashes) / (count) << "\n\n";
          }
     count++;
       }
    
       return 0;
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In what way is it not working?

    Also:
    Code:
       data *dataPtr;
    ...
          dataPtr = new data(rand() % 1000000);
    You are actually leaking memory here, since each new element is copied into the hash table from the pointer, and the allocated memory never gets deleted.
    There is no reason to use a pointer for this in the first place.

    Technically, it is also not a 6-digit number - it MAY be a 6-digit number, but it is not guaranteed - your random number may be 1, 53, 3712 or 67189 - all of which are NOT 6 digits long - there is a 10% chance of the number being shorter than 6 digits. I don't think that REALLY matters, but still that's what your problem description says you should do, and you are not doing that.

    Code:
       // initialise the random number generator
       srand(1);
    I take it you actually WANT to have the same set of random numbers for each time you run the code? If so, there's really no reason to call srand() at all - there is a default constant seed that is used if you don't call srand(). There is nothing WRONG with calling srand() like this, it just serves no genuine purpose.

    Code:
       for (count=1; count<=SIZE;) {
    ....
     count++;
       }
    Why not:
    Code:
       for (count=1; count<=SIZE;count++) {
    ....
       }
    (You may also want to move the variable declaration into the loop, as count is not used outside of the loop - at least not right now).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    30
    Hi! Thx for that Mats. And how about my fault. I couldn't find the way to solve the problem above on my post. I did change and delete
    Code:
    numClashes = hashTable.insert(*dataPtr);
    so it can compile but i can get the numClashes. This is an exercise for get knowing with hash.

    In member function ‘int linHashtable<dataType>::insert(const dataType&) [with dataType = data]’:
    hash.cpp:53: instantiated from here
    linhash.h:60: error: passing ‘const data’ as ‘this’ argument of ‘int data::getHashval(int)’ discards qualifiers

    the problems is on the line above.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sounds like you have a function getHashValue in your hash-class, which isn't declared const, and your data input is a const [and so it should be]. You need to make the getHashValue function const too.

    Something like:
    Code:
    class hash
    {
       ... 
    
       int getHashValue() const;
       ....
    };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    30
    Ah, i see. I keep focusing on the error and this "discard qualifier" a bit familiar but sometime blind.
    Thanks alot. I need to practice more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  4. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM