【问题标题】:CS50 Pset5 (Speller) Compiling IssueCS50 Pset5 (Speller) 编译问题
【发布时间】:2020-09-25 22:51:12
【问题描述】:

所以我一直在上哈佛的 CS50 课程,目前正在研究它的问题集 5,称为拼写器 (https://cs50.harvard.edu/x/2020/psets/5/speller/)

基本上我认为我已经正确填写了所有内容,但是,在尝试编译时出现此错误消息:

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)

我不确定这意味着什么,并试图弄清楚它很长一段时间,但我正在寻求解释......

我的代码是:


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

如果您想知道dictionary.h 的样子,下面是代码:

// 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

希望有人可以帮助我解决这个问题.. 任何帮助将不胜感激!

【问题讨论】:

  • 如消息所述,似乎未定义函数hash。定义它。
  • 您需要定义 hash 函数,或者如果它在其他文件中定义,则需要 #include "..." 该文件。

标签: c compiler-errors cs50


【解决方案1】:

如果您查看 load() 函数,您会看到这一行:int dicindex = hash(word);

问题在于您将hash() 更改为hash_word(),这就是您遇到错误的原因,因为代码中没有hash() 函数。最简单的做法是将unsigned int hash_word(const char* word) 改回正常状态,即unsigned int hash(const char* word),因为您不应该更改此程序中的任何函数名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2021-07-31
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多