Reputation: 347
I have a few structures with cross-pointers in my program, they are defined like this:
typedef struct
{
...
struct myvar *next;
struct myvar *prev;
...
} myvar;
typedef struct
{
...
myvar *first;
...
} variables;
And I am getting a strange error on the next piece of code:
variables *Variables;
...
Variables->first->prev->next = Variables->first;
I am using MS Visual Studio, and it says
error C2037: left of 'next' specifies undefined struct/union 'myvar'
I have set it to Compile as C Code (/TC), in C++ mode all is ok. What is the problem and is there some workaround except something like this?
tmp = Variables->first->prev;
tmp->next = Variables->first;
Upvotes: 3
Views: 561
Reputation: 39650
I constantly run into these problems, too :) Forward-declaring your typedefs should help you in your situation:
typedef struct _myvar myvar;
typedef struct _variables variables;
struct _myvar
{
...
myvar *next;
myvar *prev;
...
};
struct _variables
{
...
myvar *first;
...
};
Upvotes: 4
Reputation: 133122
Here is your problem:
typedef struct
{
...
struct myvar *next;
struct myvar *prev;
...
} myvar;
You should change it to this
typedef struct myvar_
{
...
struct myvar_ *next;
struct myvar_ *prev;
...
} myvar;
The problem is that in the first version, you're telling the compiler there is going to be a definition of struct myvar
. But you never define a struct named myvar
. You just have an anonymous struct and typedef
is as myvar
, which doesn't count. So next
and prev
remain of incomplete type.
Upvotes: 4