头文件中的 typedefed 结构在包含该文件的其他头文件中无法识别
typedefed struct in an header file is not identifiable in other header files that includes that file
我在头文件中有一个 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);
我在tokenizer.h
中多次使用list
,错误都是一样
当我尝试将 typedef
ed 结构移动到 tokenizer.h
,然后将 tokenizer.h
包含在 data_stracture.h
中时,它在 tokenizer.h
中运行良好,但是每当我在那里使用 list
时,我都会在 data_stracture.h
处遇到同样的错误。
尝试添加 struct 关键字也不起作用,生成如下错误:
tokenizer.h:12:17: error: field ‘operands’ has incomplete type
12 | struct list operands;
| ^~~~~~~~
如何防止此类错误?
您的代码 的根本问题是循环包含。
据我所知,您只需要 tokenizer.h
文件中的 struct list
声明。
你可以做的是创建一个单独的头文件,比如说structure.h
,在那里声明结构并将它包含在data_structure.h
和tokenizer.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"
.
我在头文件中有一个 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);
我在tokenizer.h
中多次使用list
,错误都是一样
当我尝试将 typedef
ed 结构移动到 tokenizer.h
,然后将 tokenizer.h
包含在 data_stracture.h
中时,它在 tokenizer.h
中运行良好,但是每当我在那里使用 list
时,我都会在 data_stracture.h
处遇到同样的错误。
尝试添加 struct 关键字也不起作用,生成如下错误:
tokenizer.h:12:17: error: field ‘operands’ has incomplete type
12 | struct list operands;
| ^~~~~~~~
如何防止此类错误?
您的代码
据我所知,您只需要 tokenizer.h
文件中的 struct list
声明。
你可以做的是创建一个单独的头文件,比如说structure.h
,在那里声明结构并将它包含在data_structure.h
和tokenizer.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"
.