0

In the file: "priority_queue.h", I have defined the struct:

#include "astar.h"
typedef struct p_q{
struct astar_node* first;
struct astar_node* last;
int size;
 }P_Q;

The file "astar.h" looks like this:

#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);

Why do I get the error: "Unknown typename 'P_Q' ?

If i redefine "funk()" to :

 int func(struct p_q* P);

the error disappears but i get the warning:"Description Resource Path Location Type 'struct p_q' declared inside parameter list will not be visible outside of this definition or declaration"

Anyone knows why?

8
  • Where is struct astar_node defined? Commented May 26, 2018 at 5:02
  • in the header "astar.h" - This header is also included in "priority_queue.h" the function: int func(P_Q* P) is implemented in "priority_queue.c" - There are no errors connected to the type "P_Q" in the implementation Commented May 26, 2018 at 5:28
  • 2
    So priority_queue.h includes astar.h and astar.h includes priority_queue.h, right? Sounds problematic. Commented May 26, 2018 at 5:30
  • That is correct. My intuition is that since I need "P_Q" in astar.h, I must include priority_queue.h in astar.h , and since I need "struct astar_node" in priority_queue.h I must include astar.h Commented May 26, 2018 at 5:33
  • Circular includes shall be avoided. Instead you can use a forward declaration in priority_queue.h, i.e. instead of including astart.h, try struct astar_node; before defining P_Q Commented May 26, 2018 at 5:36

1 Answer 1

1

Your includes are circular, i.e priority_queue.h includes astar.h which includes priority_queue.h. That is something you should avoid.

Try changing:

#include "astar.h"
typedef struct p_q{
    struct astar_node* first;
    struct astar_node* last;
    int size;
} P_Q;

into

// Forward declaration
struct astar_node;

typedef struct p_q{
    struct astar_node* first;
    struct astar_node* last;
    int size;
} P_Q;

You can do this because you only use pointers to struct astar_node inside priority_queue.h. So the compiler doesn't need to know what is inside struct astar_node. All it needs to know is that there is a struct named astar_node somewhere in your project.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.