“sizeof"对不完整类型的无效应用 [英] invalid application of ‘sizeof’ to incomplete type

查看:81
本文介绍了“sizeof"对不完整类型的无效应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的makefile文件全部:试试

This is my makefile file 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
}

在主文件中出现错误信息

it occurs error message in the main file

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

main.c: In function ‘main’: main.c:7:35: error: invalid application of ‘sizeof’ to incomplete type ‘node {aka struct node}’ node* x = (node*) malloc(sizeof(node));

知道为什么我的代码有错误吗?

have any idea why my code has an error?

推荐答案

你的 main.c 没有 node 的定义,只是名称的声明无需定义结构.您要么需要在 .h 文件中包含定义,以便 trie.cmain.c 都可以看到它,或者您需要提供一个分配器方法(在 trie.h 中声明,在 trie.c 中定义)可以执行节点<的定义感知分配(以及可能的初始化)/code> 在可以访问其他不透明类型定义的地方.

Your main.c doesn't have a definition of node, just a declaration of the name without defining the structure. You either need to include the definition in the .h file so both trie.c and main.c can see it, or you need to provide an allocator method (declared in trie.h, defined in trie.c) that can perform a definition aware allocation (and possibly initialization) of a node in a place that has access to the definition of the otherwise opaque type.

这篇关于“sizeof"对不完整类型的无效应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆