February 4th, 2013, 05:06 PM
What's wrong with the syntax?
i'm trying to define the following:
but i'm getting a compilation error: "unknown type name 'list'".
how else can i define a nested struct of the same type?
thanks in advanced!
February 4th, 2013, 05:21 PM
There may be other ways. I actually had to look this one up, since my schooling in linked lists was in my pre-C days, and since C++'s approach to a similar problem is slightly different syntactically.
For the field, next, the compiler has to already know about list, but it doesn't yet. However, it does know about struct _list. And since in subsequent code and declarations list will already be known to be struct _list, next should be compatible with pointers declared as list* .
typedef struct _list
struct _list* next;
Hopefully there are better solutions, but I just can't think of any.
Last edited by dwise1_aol; February 4th, 2013 at 05:25 PM.
February 4th, 2013, 05:31 PM
yup. thanks man.
now it compiles without any errors.
only now, Eclipse warns me every time i try to assign from "struct list" type to "list" type and vise versa, since it sees them as different types ("assignment from incompatible pointer type").
nothing i can't live with, though...
February 4th, 2013, 06:12 PM
A preprocessor alternative (much despised by some) I use:
#define NODE struct node_structure
[/code] are essential for python code and Makefiles!
February 4th, 2013, 06:33 PM
Did you notice that struct _list has a leading underscore in front of it, whereas the typedef doesn't. Just declare all your variables to be of type list instead of struct _list and you should have no warnings.
Originally Posted by so.very.tired
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
February 5th, 2013, 06:11 AM
An alternative is to use C++ compilation where the typedef nonsense is unnecessary:
Defines a type list that does not require the struct keyword to be used everywhere it is referenced.
That said the C++ standard library includes a list container class that would probably make this struct redundant in any case.