gpt4 book ai didi

c - 头文件中的typedefed结构在包含该文件的其他头文件中无法识别

转载 作者:行者123 更新时间:2023-12-02 10:56:24 24 4
gpt4 key购买 nike

我在头文件中有一个typedef ed结构
data_stracture.h:

#if !defined(__DATA__STRUCTURE__HEADER__)
#define __DATA__STRUCTURE__HEADER__
#include <stdbool.h>
#include "tokenizer.h"
/*arraylist implemtation*/
#define INIT_SIZE 4
#define GROWING_FACTOR 2
typedef struct
{
char** enumerable_items;
int size;
int first_free_index;
}list;
list init_list(void);
bool add_to_end(list collection, char* added_item);
char* get_item_at_index(list access_list, int access_index);
void free_list(list memory_to_free);
list remove_item(list, char *);
/*more....*/
#endif
在第二个文件中,我包括 file_1.h,我声明了一个方法,其中的一个参数来自 list结构类型。
tokenizer.h:
#if !defined(__TOKENIZER__HEADER__)
#define __TOKENIZER__HEADER__
#include <stdbool.h>
#include "data_structure.h"
#define WORD_SIZE 24
typedef enum {none, asm_command, ignore, instruction}asm_line_type;

typedef struct
{
char *label;
char *op_name;
struct list operands;
bool is_instruction;
unsigned int adress : WORD_SIZE;
int line_number;
}asm_line_data;
#define NOT_FOUND {NULL, none, -1, false, -1}

bool match(char* command, char* match_template);
struct asm_command_syntax get_instruction_operand_amount(char *command, struct asm_command_syntax match_template);
asm_line_data *get_data(struct asm_command_syntax* command_syntax, struct list commands);
asm_line_data *tokenize(struct list string_commands, long *);
void validate_commas(struct list);
int get_word_amount(char* spaces_text);
struct asm_command_syntax classify(char* command);
#endif
这两个头文件都包含防护。
使用 gcc( ansi C)编译程序时,出现以下编译器错误:
file_2.h:22:25: error: unknown type name ‘list’
22 | void foo(list);
我已经在 list中多次使用了 tokenizer.h,并且错误是相同的。
当我尝试将 typedef ed结构移动到 tokenizer.h,然后将 tokenizer.h包括在 data_stracture.h中时,它在 tokenizer.h中可以正常工作,但是当我在 data_stracture.h中使用 list时,我却遇到了相同的错误。
尝试添加struct关键字也不起作用,从而产生如下错误:
tokenizer.h:12:17: error: field ‘operands’ has incomplete type
12 | struct list operands;
| ^~~~~~~~

如何防止此类错误?

最佳答案

您的代码@StoryTeller-Unslander Monica stated的基本问题是循环包含。
据我所知,您只需要struct list文件中的tokenizer.h声明。
您可以做的是创建一个单独的头文件,假设为structure.h,在那里声明结构并将其包含在data_structure.htokenizer.h中。
structure.h

typedef struct list{ //adding list here means you can use both list and struct list
//...
}list;
data_structure.h
#include "structure.h"
并删除 #include "tokenizer.h"以及结构声明。
tokenizer.h
#include "structure.h"
并删除 #include "data_structure.h"

关于c - 头文件中的typedefed结构在包含该文件的其他头文件中无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62771548/

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