- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,我上了哈佛的CS50类(class),目前正在研究称为“拼写检查器(https://cs50.harvard.edu/x/2020/psets/5/speller/)”的问题5
基本上,我认为我已经正确填满了所有内容,但是,在尝试编译此错误消息时出现:
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
希望有人可以帮助我。
最佳答案
如果查看load()
函数,则有以下一行:int dicindex = hash(word);
。
问题是您将hash()
更改为hash_word()
,这就是为什么出现错误的原因,因为代码中没有hash()
函数。最简单的方法是将unsigned int hash_word(const char* word)
更改回正常的unsigned int hash(const char* word)
,因为您没有打算在此程序中更改任何函数名。
关于c - CS50 Pset5(Speller)编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64072204/
我是编程的新手,我一直在尝试完成 cs50 类(class),而不只是复制其他人的代码并试图理解为什么我的代码不起作用。我目前被困在 pset5 中(我已经待了几天),代码编译正常,但无法正常工作并且
我正在尝试释放一个特里树,但在运行 valgrind 后我仍然可以看到大量已用内存。有人能指出我正确的方向吗?我能改变什么?我试着把它画在纸上,逻辑上它对我来说是有意义的,但显然它不能正常工作。感谢您
因此,我上了哈佛的CS50类(class),目前正在研究称为“拼写检查器(https://cs50.harvard.edu/x/2020/psets/5/speller/)”的问题5 基本上,我认为我
我刚刚完成了 cs50 Speller,代码通过了所有其他测试,但它一直告诉我“您正在尝试使用可能没有值的 8 字节变量”,在“while”行上拼写检查部分的代码。 我看过其他人对此问题的回答,但我仍
我已经按照下面给定的链接在我的 .NET 应用程序中实现了拼写检查器功能: Speller page 我在我的应用程序中复制了拼写器页面,但出现错误: The HTTP verb POST used
我是一名优秀的程序员,十分优秀!