简体   繁体   中英

CS50 Pset5 (Speller) Compiling Issue

So I've been taking Harvard's CS50 class and currently am working on it's Problem Set 5 called speller ( https://cs50.harvard.edu/x/2020/psets/5/speller/ )

Basically I think that I have filled everything correctly, however, when trying to compile this error message appears :

In function `check':/home/ubuntu/pset5/speller/dictionary.c:33: undefined reference to `hash' dictionary.o: In function `load': /home/ubuntu/pset5/speller/dictionary.c:90: undefined reference to `hash' clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

I am not sure what this means, and tried to figure it out for quite some time, but I'm reaching out for an explanation...

My code is :


#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>


#include "dictionary.h"


int word_count = 0;

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
   unsigned int h = hash(word); 
   
   node *cursor = table[h];
   
   while(cursor != NULL)
   {
       if(strcasecmp(word, cursor -> word) ==0)
       {
           return true;
       }
       else
       {
           cursor = cursor -> next;
       }
   }
    
    return false;
}

// Hashes word to a number
// This hash function was adapted by Neel Mehta from 
// http://stackoverflow.com/questions/2571683/djb2-hash-function.

unsigned int hash_word(const char* word)
 {
     unsigned long hash = 5381;

     for (const char* ptr = word; *ptr != '\0'; ptr++)
     {
         hash = ((hash << 5) + hash) + tolower(*ptr);
     }

     return hash %26;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *dic = fopen(dictionary, "r");
    char word[LENGTH + 1];
    
    if(dic == NULL)
    {
        unload();
        return false; 
    }
    
    while (fscanf(dic,"%s",word) != EOF)
    {
        node *sllnode = malloc(sizeof(node));
        if( sllnode == NULL)
        {
            return false;
        }
    strcpy(sllnode -> word, word);
    word_count++;
    
    int dicindex = hash(word);
    
    if(table[dicindex] == NULL)
    {
        sllnode -> next = NULL;
    }
    else
    {
        sllnode -> next = table[dicindex];
    }
    
    table[dicindex]= sllnode;
    }
fclose(dic);
    // check here whethet to free memory space or not (maybe needs to be freed at very end)
    
return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    return word_count;
    return 0;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for(int i = 0; i < N ; i++)
    {
        node *cursor = table[i];
        
        while(cursor)
   {
       node *tmp = cursor;
       cursor = cursor -> next;
       free(tmp);
   }
   
}
   
    return true;
}

If you would like to know what dictionary.h looks like, here is the code for that:

// Declares a dictionary's functionality

#ifndef DICTIONARY_H
#define DICTIONARY_H

#include <stdbool.h>

// Maximum length for a word
// (e.g., pneumonoultramicroscopicsilicovolcanoconiosis)
#define LENGTH 45

// Prototypes
bool check(const char *word);
unsigned int hash(const char *word);
bool load(const char *dictionary);
unsigned int size(void);
bool unload(void);

#endif // DICTIONARY_H

Hoping someone could help me with this.. Any help would be appreciated!

If you look in your load() function, you have this line: int dicindex = hash(word); .

The issue with this is that you changed hash() into hash_word() which is why you are having the error because there is no hash() function in the code. Easiest thing to do is to change unsigned int hash_word(const char* word) back to normal which was unsigned int hash(const char* word) as you weren't suppose to change any function name in this program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM