首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C转发声明导致未知的类型错误:我如何得到类型、结构等的顺序?

C转发声明导致未知的类型错误:我如何得到类型、结构等的顺序?
EN

Stack Overflow用户
提问于 2022-10-14 12:11:23
回答 2查看 27关注 0票数 1

我知道有类似的帖子,但我很难掌握它的诀窍:我有一个.c和一个.h文件:

graph.c

代码语言:javascript
复制
#ifndef graph
#define graph

/*1. standard headers*/
#include <stdbool.h>

/*2. user-def. headers*/
//#include ".."

/*3. global macros*/
//#define GLOB_MACRO … 

/*4. global type decls.*/

struct graph;
typedef struct graph graph_t;

graph_t* graph_create(int n);
void graph_free(graph_t *g);

void graph_insert_edge(graph_t *g, int source, int target, double weight);
void graph_remove_edge(graph_t *g, int source, int target);

bool graph_has_edge(graph_t *g, int source, int target);
double graph_get_weight(graph_t *g, int source, int target);

void graph_print(graph_t *g);

/*5. glob. var. decls.*/
//extern … glob_var; 

/*6. glob. func. decls*/
//[extern] … glob_func(…);

#endif

graph_m.c

代码语言:javascript
复制
// Driver Code
int main()
{
    printf("test");
 
    return 0;
}

但是我得到了这些编译器错误:

代码语言:javascript
复制
gcc  -MM graph_m.c > .depend
gcc -pedantic -Wall -Wextra -Wvla -std=c11 -ggdb3    -c -o graph_m.o graph_m.c
In file included from graph_m.c:14:
graph.h:15:13: error: expected '{' before ';' token
   15 | struct graph;
      |             ^
graph.h:16:22: warning: useless storage class specifier in empty declaration
   16 | typedef struct graph graph_t;
      |                      ^~~~~~~
graph.h:18:1: error: unknown type name 'graph_t'; use 'struct' keyword to refer to the type
   18 | graph_t* graph_create(int n);
      | ^~~~~~~
      | struct
graph.h:19:17: error: unknown type name 'graph_t'; did you mean 'graph'?
   19 | void graph_free(graph_t *g);
      |                 ^~~~~~~
      |                 graph
graph.h:21:24: error: unknown type name 'graph_t'; did you mean 'graph'?
   21 | void graph_insert_edge(graph_t *g, int source, int target, double weight);
      |                        ^~~~~~~
      |                        graph
graph.h:22:24: error: unknown type name 'graph_t'; did you mean 'graph'?
   22 | void graph_remove_edge(graph_t *g, int source, int target);
      |                        ^~~~~~~
      |                        graph
graph.h:24:21: error: unknown type name 'graph_t'; did you mean 'graph'?
   24 | bool graph_has_edge(graph_t *g, int source, int target);
      |                     ^~~~~~~
      |                     graph
graph.h:25:25: error: unknown type name 'graph_t'; did you mean 'graph'?
   25 | double graph_get_weight(graph_t *g, int source, int target);
      |                  graph
graph_m.c:24:1: warning: unnamed struct/union that defines no instances
   24 | } graph;
      | ^
make: *** [<builtin>: graph_m.o] Error 1
root@8c9bf05a5390:/home/swo3/src/graph# In file included from graph_m.c:14:
graph.h:15:13: error: expected '{' before ';' token
   15 | struct graph;
      |             ^
graph.h:16:22: warning: useless storage class specifier in empty declaration
   16 | typedef struct graph graph_t;
      |                      ^~~~~~~
graph.h:18:1: error: unknown type name 'graph_t'; use 'struct' keyword to refer to the type
   18 | graph_t* graph_create(int n);
      | ^~~~~~~
      | struct
make: *** [<builtin>: graph_m.o] Error 1union that defines no instances?e weight);

我是不是弄错命令了?我在运行中有ty胡枝子和struct,对于头文件的实际实现有不同的文件名,其中我想在后台为graph.h、graph_m.c和另一个数据结构做两个数据结构。

EN

回答 2

Stack Overflow用户

发布于 2022-10-14 12:16:55

宏名与其他名称发生冲突。

你先从这个开始:

代码语言:javascript
复制
#define graph

然后你就有了这个

代码语言:javascript
复制
struct graph;

由于宏做了直接的令牌替换,在预处理后,上面的行变成:

代码语言:javascript
复制
struct ;

这是无效的。

您需要更改宏名,这样就不会发生冲突。按照惯例,宏通常是大写的,头保护宏通常以_H结尾。因此,将前两行更改为:

代码语言:javascript
复制
#ifndef GRAPH_H
#define GRAPH_H
票数 4
EN

Stack Overflow用户

发布于 2022-10-14 12:19:05

#define graph之后,宏处理将struct graph;替换为struct ;

为预处理器保护使用不同的标识符,如graph_h

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74069027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档