在文件:“priority_queue.h”中,我定义了结构:
#include "astar.h"
typedef struct p_q{
struct astar_node* first;
struct astar_node* last;
int size;
}P_Q;
文件“astar.h”如下所示:
#include "priority_queue.h"
typedef struct astar_node{
struct astar_node* next;
struct astar_node* prev;
struct astar_node* PQ_next;
struct astar_node* PQ_prev;
int x;
int y;
int c;
int h;
int tot_cost;
}Astar_node;
int func(P_Q* P);
为什么我收到错误:“未知类型名‘P_Q’?
如果我将“funk()”重新定义为:
int func(struct p_q* P);
错误消失,但我收到警告:“描述资源路径位置类型 在参数列表内声明的“struct p_q”在此定义或声明之外将不可见”
有人知道为什么吗?
最佳答案
您的包含是循环的,即 priority_queue.h
包含 astar.h
,其中包含 priority_queue.h
。这是你应该避免的事情。
尝试改变:
#include "astar.h"
typedef struct p_q{
struct astar_node* first;
struct astar_node* last;
int size;
} P_Q;
进入
// Forward declaration
struct astar_node;
typedef struct p_q{
struct astar_node* first;
struct astar_node* last;
int size;
} P_Q;
您可以这样做,因为您只使用指针来priority_queue.h
内的struct astar_node
。因此编译器不需要知道 struct astar_node 内部有什么。它需要知道的是您的项目中某处有一个名为 astar_node
的结构。
关于c - "Error: unknown type name"- 在函数声明中使用结构别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50539450/