[英]Compiler error using struct and typedef
我的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
}
我遇到了編譯器錯誤: 編譯器錯誤C2275和編譯器錯誤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中使用此標頭時,編譯器在struct node
會遇到問題,因為它不是在此上下文中定義的。 它僅在node.c中定義,但是編譯器無法從list.c中看到該定義。
由於您僅使用指向node_t
指針,請嘗試將node.h更改為如下所示:
// node.h
struct node;
typedef struct node node_t;
現在,您已經預先聲明了一種名為struct node
的數據類型。 對於編譯器來說,足夠的信息來處理typedef
並創建指針,但是由於尚未完全定義,因此您無法聲明struct node
類型的對象或取消引用struct node
指針。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.