1

I have the following files in my VS project:

// list.h

#include "node.h"

typedef struct list list_t;

void push_back(list_t* list_ptr, void* item);


// node.h

typedef struct node node_t;


// node.c

#include "node.h"

struct node
{
   node_t* next;
};


// list.c

#include "list.h"


struct list
{
    node_t* head;
};

void push_back(list_t* list_ptr, void* item)
{
   if(!list_ptr)
       return;

   node_t* node_ptr; // Here I have two compiler errors
}

I have the compiler errors: Compiler Error C2275 and Compiler Error C2065.

Why? How can i fix this problem?

12
  • I get a compiler error at list_t. That type has not been defined. Commented Jul 10, 2012 at 11:35
  • 1
    Have you #included the header files in list.c? Commented Jul 10, 2012 at 11:36
  • 1
    @Nick: Put it another way; if you create a new project with just the above code, do you get the exact same error message? This is very important; if you're asking questions about compiler error messages/syntax errors, you need to be precise about what code you're using, otherwise people will be left guessing. Commented Jul 10, 2012 at 11:46
  • 1
    What @OliCharlesworth hints at is that its pretty hard to answer this kind of question without an SSCCE. Commented Jul 10, 2012 at 11:53
  • 1
    Side note: try to avoid using the suffix _t. Besides the fact that it is useless, it is also reserved by POSIX. You can safely write typedef struct node node and use node instead of introducing node_t. Commented Jul 10, 2012 at 16:24

1 Answer 1

1

Here's what list.h looks like after the pre-processor handles the #include lines (some comments excluded):

// list.h 

typedef struct node node_t;

typedef struct list list_t; 

void push_back(list_t* list_ptr, void* item); 

When you use this header inside list.c, the compiler has problems with struct node because it is not defined in this context. It is only defined in node.c, but the compiler can't see that definition from list.c.

Since you're only using pointers to node_t, try changing node.h to look like this:

// node.h     

struct node;
typedef struct node node_t;

Now, you've pre-declared that there is a data type called struct node. It's enough information for the compiler to handle typedefs and create pointers, but since it hasn't been completely defined you can't declare an object of type struct node or de-reference a struct node pointer.

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.