gpt4 book ai didi

使用 struct 和 typedef 的编译器错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:13 29 4
gpt4 key购买 nike

我的 VS 项目中有以下文件:

// list.h

#include "node.h"

typedef struct list list_t;

void push_back(list_t* list_ptr, void* item);


// node.h

typedef struct node node_t;


// node.c

#include "node.h"

struct node
{
node_t* next;
};


// list.c

#include "list.h"


struct list
{
node_t* head;
};

void push_back(list_t* list_ptr, void* item)
{
if(!list_ptr)
return;

node_t* node_ptr; // Here I have two compiler errors
}

我有编译器错误:Compiler Error C2275Compiler Error C2065 .

为什么?我该如何解决这个问题?

最佳答案

这是预处理器处理 #include 行后 list.h 的样子(不包括一些注释):

// list.h 

typedef struct node node_t;

typedef struct list list_t;

void push_back(list_t* list_ptr, void* item);

当您在 list.c 中使用此 header 时,编译器会遇到 struct node 问题,因为它未在此上下文中定义。它仅在 node.c 中定义,但编译器无法从 list.c 中看到该定义。

由于您只使用指向 node_t 的指针,请尝试将 node.h 更改为如下所示:

// node.h     

struct node;
typedef struct node node_t;

现在,您已经预先声明了一种名为struct node 的数据类型。它足以让编译器处理 typedef 和创建指针,但由于它尚未完全定义,因此您无法声明 struct node 或 de 类型的对象-引用一个struct node指针。

关于使用 struct 和 typedef 的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11412425/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com