“sizeof”对不完整类型的无效应用
invalid application of ‘sizeof’ to incomplete type
这是我的 makefile 文件
全部:特里
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}”的无效应用
节点* x = (节点*) malloc(sizeof(node));
知道为什么我的代码有错误吗?
你的main.c
没有node
的定义,只是声明了名字,没有定义结构。您要么需要在 .h
文件中包含定义,以便 trie.c
和 main.c
都可以看到它,要么您需要提供分配器方法(在 trie.h
中声明,定义在 trie.c
) 中,它可以在可以访问其他不透明类型的定义的地方执行 node
的定义感知分配(可能还有初始化)。
尝试包含包含相关结构的头文件。
这是我的 makefile 文件 全部:特里
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}”的无效应用 节点* x = (节点*) malloc(sizeof(node));
知道为什么我的代码有错误吗?
你的main.c
没有node
的定义,只是声明了名字,没有定义结构。您要么需要在 .h
文件中包含定义,以便 trie.c
和 main.c
都可以看到它,要么您需要提供分配器方法(在 trie.h
中声明,定义在 trie.c
) 中,它可以在可以访问其他不透明类型的定义的地方执行 node
的定义感知分配(可能还有初始化)。
尝试包含包含相关结构的头文件。