Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My question is:
Open a file in your main. Your file has one word in each line. I'm supposed to read a file sum up the ascii values of each word, the sum mod the hash size will be my key and i'm supposed to take care of the collisions with linear probing. for linear probing I need to create a linear probing class. I have two questions:

a) I do not understand this error message.
"undefined reference to `LinearProbing::HashFunction(std::string, int, int)"
I do not understand why it says that.

b) Have I done the Insert method right? My logic is that if the spot of a given key is empty, insert the word in that key or index. If not empty, go one step down and insert the word there.

Any input on this matter would help me greatly!! and Please no sarcastic remarks! Thank you!!!

What I have tried:

C#
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;
//Global variables
int HASHSIZE=100;
string HashTable[100];

class LinearProbing{

int HashFunction(string,int,int);
void InsertWord(string);
int SearchWord(string);
};


int main(){
string word;
string line;
fstream file;
int index;

//User Input
cout<<"Enter a word you would like to search: ";
cin>>word;

//Reading File
file.open("text.txt");
while(file){                      //http://www.cplusplus.com/forum/beginner/146982/
 getline(file,line);
 }
file.close();

}

int HashFunction(string key, int index,int HASHSIZE){
int sum=0;
for(int i=0;i<index;i++){
    sum=sum+key[i];
}
return sum%HASHSIZE;
}

void LinearProbing::InsertWord(string word){
int key= LinearProbing::HashFunction(word,word.length(),HASHSIZE);
for (int i = 0; i < HASHSIZE; i++){
        if (HashTable[key].empty()==true){//if the spot is empty, fill the space with the word
            HashTable[key] = word;
            return;
        }
        else{
            HashTable[key+1] = word;
        }
    }
}

int LinearProbing::SearchWord(string word){
int key=LinearProbing::HashFunction(word,word.length(),HASHSIZE);
for (int i = 0; i < HASHSIZE; i++){
        if (HashTable[(key + i)%HASHSIZE] == word){
            cout<<"The word you're looking for is in "<<key+i<<"position";
        }
    }
    cout << "Value not contained in table";
    return 0;
}
Posted
Updated 3-Dec-16 0:27am
Comments
[no name] 13-Oct-16 12:47pm    
It means that it can't find the function because you left out the LinearProbing:: when you implemented the function.
PinkPrint 13-Oct-16 13:09pm    
But I have used LinearProbing:: in both times whenever I needed to call the HashFunction??

void LinearProbing::InsertWord(string word){
int key= LinearProbing::HashFunction(word,word.length(),HASHSIZE);

int LinearProbing::SearchWord(string word){
int key=LinearProbing::HashFunction(word,word.length(),HASHSIZE);
jeron1 13-Oct-16 13:14pm    
Like NotPoliticallyCorrect said,

from
int HashFunction(string key, int index,int HASHSIZE){... 


to
int LinearProbing::HashFunction(string key, int index,int HASHSIZE){...


have you tried it?
PinkPrint 13-Oct-16 22:58pm    
Oh I see it now. Thank you very much!!

1 solution

Probably, like this:

C++
int LinearProbing::SearchWord(string word){
LinearProbing lp;
int key=lp.HashFunction(word,word.length(),HASHSIZE);
for (int i = 0; i < HASHSIZE; i++){
        if (HashTable[(key + i)%HASHSIZE] == word){
            cout<<"The word you're looking for is in "<<key+i&lt;&lt;"position";&lt;!-- newline="" --="">        }
    }
    cout << "Value not contained in table";
    return 0;
}


Use objects.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900