这是我的makefile文件all: trie
trie: trie.o main.o
gcc trie.o main.o -o trie -std=c11 -g -Wall
trie.o: trie.c trie.h
gcc -c trie.c -o trie.o -std=c11 -g -Wall
main.o: main.c trie.h
gcc -c main.c -o main.o -std=c11 -g -Wall
clean:
rm -f *.o trie
和头文件
#ifndef TRIE_H
#define TRIE_H
struct node;
typedef struct node node;
//insert a word in a leaf
void insert(char* word, node* leaf);
#endif //TRIE_H
和trie.c文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "trie.h"
struct node {
char* data;
node* child[127];
};
void insert (char* word, node* leaf) {
node* curr = leaf;
for (size_t i = 0; i < strlen(word); i++) {//start from beginning of char to end
if (curr == NULL) {
curr = (node*)malloc(sizeof(node)); // if it's null, creating new node
curr->data = "";
}
curr = curr->child[(int) word[i]];
}
curr->data = word; // set last node stored the word
}
它在主文件中出现错误消息
#include <stdio.h>
#include <stdlib.h>
#include "trie.h"
int main() {
node* x = (node*) malloc(sizeof(node));
insert("hi", x);
return 0;
}
这是错误消息:
main.c:在函数‘main’中: main.c:7:35:错误:将‘sizeof’应用于不完整的类型‘node {aka struct node}’node* x= (node*) malloc(sizeof(node));
你知道为什么我的代码有错误吗?
发布于 2017-05-11 00:43:15
您的main.c
没有node
的定义,只是声明了名称而没有定义结构。您要么需要将定义包含在.h
文件中,以便trie.c
和main.c
都能看到它,要么需要提供一个分配器方法(在trie.h
中声明,在trie.c
中定义),该方法可以在有权访问其他不透明类型的定义的位置执行node
的定义感知分配(可能还包括初始化)。
发布于 2021-11-18 18:43:41
尝试包含包含相关结构的头文件。
https://stackoverflow.com/questions/43904700
复制相似问题