错误:预期的声明说明符或'list_node'之前的'...'

pvi*_*nis 7 c typedef header declaration

我有一个catalog.h文件

typedef struct node* list_node;
struct node
{
    operationdesc op_ptr;
    list_node next;
};
Run Code Online (Sandbox Code Playgroud)

和一个parser.h

#include "catalog.h"

int parse_query(char *input, list_node operation_list);
Run Code Online (Sandbox Code Playgroud)

两个标题有#ifndef,#define,#endif.编译器给我这个错误:expected declaration specifiers or ‘...’ before ‘list_node’在parse_query行上.怎么了?我试着把typedef放在parser.h中,没关系.当typedef在catalog.h中时,为什么会出现此错误?

Tim*_*nes 6

错误是这个(来自你的评论):

我在catalog.h中有一个#include"parser.h".我删除它,现在它正常编译...

假设它#include "parser.h"在typedef之前catalog.h,并且您有一个包含catalog.h之前的源文件parser.h,那么在编译器包含时parser.h,typedef尚不可用.最好重新排列头文件的内容,这样就不会有循环依赖.

如果这不是一个选项,您可以确保包含这两个文件的任何源文件包括parser.h第一个(或仅包括).


San*_*hat 0

尝试这个catalog.h

typedef struct node_struct {
     operationdesc op_ptr;
     struct node_struct* next;
} node;

typedef node* list_node;
Run Code Online (Sandbox Code Playgroud)

  • 以两个下划线开头的名称是保留的,请勿使用它们。 (8认同)