![]() |
|
#1
|
|||
|
|||
Linked list: Dereferencing to incomplete typeI have been experimenting with linked lists and was making a small program to sort of sum up all what I learned so far.
I'm getting a weird error, Dereferencing pointer to incomplete type from this line. CPP / C++ / C Code:
Head is a pointer to a struct which was declared so. CPP / C++ / C Code:
And initialized so. CPP / C++ / C Code:
I'm also getting warnings for assignment from incompatible pointer type on lines like these. CPP / C++ / C Code:
From what I gathered it is because of the self reference in the structure and I shouldn't use typedef, is there a work around so that I can keep it as it is or what is the proper way to do this? Thanks Max |
|
#2
|
|||
|
|||
Re: Linked list: Dereferencing to incomplete typeQuote:
You must use a tag for the struct. The tag can have the same name as the type (but I usually make it different, just for my own sensibilities). CPP / C++ / C Code:
In my example, Linked_List_ is the tag. Note that the pointer member inside the struct definition uses the tag. It would be legal, and many people would prefer, to write CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: Linked list: Dereferencing to incomplete typeThanks, I have fixed it, could you explain the behavior or point to an article about this?
I have tried to google this but didn't find anything other than a few articles saying do it this way with no explanation. Max |
|
#4
|
|||
|
|||
Re: Linked list: Dereferencing to incomplete typeQuote:
The point is, that if you want to refer to a struct in some of the definitions of its members or in some later program declarations, then you must use a tag: CPP / C++ / C Code:
point_ is the tag. Once this declaration has been made, then you can do things like this: CPP / C++ / C Code:
Etc. If you don't want to type the "struct" every time, then you can do this: CPP / C++ / C Code:
Then you could write CPP / C++ / C Code:
etc. Now, as I previously mentioned, I like to give the tag a different name than the data type itself. This is not necessary, since the namespace for tags does not overlap the namespace for type definitions. So everything that I have written so far could have been written; CPP / C++ / C Code:
If you want to have a pointer to the same kind of struct as a member of the struct, then you must use the tag in the definition of the struct: CPP / C++ / C Code:
If you want to put everything (the declaration and the typedef) in the same statement, then you can do it, but remember that you have to use the tag; CPP / C++ / C Code:
Or CPP / C++ / C Code:
This is one of the "tricks of the trade" that many people don't try to understand; they just use something like the last thing that I wrote, and they treat it as an idiom. I believe that idioms are useful in the short term, but for the long haul, understanding trumps idioms every time. (That's just my opinion, of course. See footnote.) An on-line reference: http://www-ccs.ucsd.edu/c/. I use this a lot to make sure I understand standard library function usage, but they have some other language features also. So, click on declarations Then scroll down through "Visibility and Namespaces" on your way to "Tags" and "Type Definitions". Regards, Dave Footnote: "The opinions expressed here are not necessarily my own. It's these dang voices in my head." ---davekw7x |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| game problem using linkedListType.h and random function | eureka360 | C++ Forum | 5 | 16-Feb-2007 10:48 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| linked list error message | Krandygrl00 | C++ Forum | 4 | 22-Jun-2005 14:13 |
| search linked list | itsmecathys | C++ Forum | 20 | 18-Apr-2005 01:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The