error: expected declaration specifiers or ‘…’ befo

2019-04-07 09:24发布

I have a catalog.h file with this

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

and a parser.h with this

#include "catalog.h"

int parse_query(char *input, list_node operation_list);

Both headers have #ifndef, #define, #endif. The compiler gives me this error: expected declaration specifiers or ‘...’ before ‘list_node’ on the parse_query line. What's the matter? I tried to put the typedef in parser.h, and it's fine. Why do I get this error when the typedef is in catalog.h?

2条回答
趁早两清
2楼-- · 2019-04-07 10:05

Try this for catalog.h:

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

typedef node* list_node;
查看更多
SAY GOODBYE
3楼-- · 2019-04-07 10:09

The error is this (from your comment):

I had an #include "parser.h" in the catalog.h. I removed it, and now it compiles normally...

Assuming that #include "parser.h" was before the typedef in catalog.h, and you have a source file that includes catalog.h before parser.h, then at the time the compiler includes parser.h, the typedef isn't available yet. It's probably best to rearrange the contents of the header files so that you don't have a circular dependency.

If this isn't an option, you can ensure that any source files that include these two files include parser.h first (or only).

查看更多
登录 后发表回答