   C - Need some help solving circular dependency issue.
  Hi guys,I've got a bit of a problem. I've done a quick search on circular dependency, but what I've found doesn't seem to solve my problem.I have a header file (draw.h) that is included by linkedList.h, because linkedList.h needs some structures that draw.h defines. draw.h on the other hand has a structure in it that has a LinkedList type (defined in linkedList.h) as a member.This is generating some really weird compiler errors. Example:"syntax error : missing ';' before '*'"And I get 187 of these bad boys. I am using include guards. I don't know if it's important, but both draw.h and linkedList.h are included in other .cpp files that only includes either one, or both.I hope someone can give me a tip here. I really don't want to slam everything into one header file, it'd be a huge mess.Cheers,Brick   
  linkedlist.h should not be including draw.h.  I know your first instinct is going to be to respond telling me why you need this, but try to stop yourself before you make that post.  :)   I hope that this stuff used by linkedlist.h actually has nothing to do with drawing, in which case what you should do is make a new header file called commonstuff.h or whatever.  Include *that* header file from draw.h and from linkedlist.h.If, on the other hand, it does have to do with drawing, then you need to rethink why a generic LinkedList class needs to know about drawing.  It shouldn't.  It should just be a linked list, nothing else.   ;  Read this.Though I cannot think why a linked list would need to draw anything. Try to design with the single responsibility principle in mind. Still, solving the immediate code problem will be a benefit for you for the future, then afterwards you can decouple the linked list from the drawing.   ;  Edit: I see some people have posted some very logical responses while I was typing this but I'll leave it all the sameYou say C but you mention .cpp files so I'm guessing you mean c++. You need to pre declare some of the things;if you have two classes A and B and they each have an object of one another:// A.h#include "B.h"class A{    A(){}    B b;};// B.h#include "A.h"class B{    B(){}    A a;};Its not going to work because of cyclic dependancies. A fairly simple way to break the cycle is to make both variables pointers instead and create them with new in the constructor. You can then use forward declarations to allow it to compile:// A.h// forward declaration of Bclass B;class A{    A();    B *b;};// A.cpp#include "B.h"A::A(){    b = new B;]// B.h// forward declaration of Aclass A;class B{    B();    A *a;};// B.cpp#include "A.h"B:B(){    a = new A;};  To clarify what I'm doing:I have a PointStruct...which is used to store information about the points that are to be drawn in a coord system.PointStruct is in draw.h. I also draw curves that go through a set of points. So I have a CurveStruct for each curve. Each of these curves has a linked List as a member. A linked list of pointers to PointStruct's. Imo this is all fine and dandy. But in linkedList.h I have: typedef PointStruct* dataType.Just to clarify, I'm using C, sorry about the .cpp...my mistake...so I don't have the option of using templates. I could make dataType void* and work it from there, but I've never had this problem before so I think it'd be good to solve it the way software engineers do when they encounter it.I read part of the article posted in the link in this thread. Out of curiosity, is it possible to do forward declarations in C like you can in C++?I'm used to doing something like this in C:typedef struct _MyStruct{    struct _MyStruct myStruct;    ...} MyStruct;But I'm still getting the errors.   ;  I think what you want to do is put PointStruct in a separate file. And rename linkedList.h to something that clearly indicates that it's specific to PointStruct, and possibly further separate that from CurveStruct. Basically, what rip_off said.   ;  Yea, thats basically what I did now, everything is working fine. That was never really the problem, but I guess its the easiest option. I just wanted to avoid making new files. But now I'll know what to look for if I really do encounter this problem again and the solution isn't that simple. Thanks for the help.   ;Quote:I just wanted to avoid making new files.This is probably the number one reason for ugly, no good, unmanageable code in any project. If a function doesn't fit in about 1-2 screen-sized pages worth of code it's doing too much, split it up. If you can't page-down scroll through a source/header file within ~3-4 seconds, consider splitting it up.There's absolutely nothing wrong with many, well-named and well-defined files in a project.I once wrote a single function so enormous the assembler ran out of registers to use.. :)   ;Quote:Original post by BrickInTheWallTo clarify what I'm doing:I have a PointStruct...which is used to store information about the points that are to be drawn in a coord system.PointStruct is in draw.h. I also draw curves that go through a set of points. So I have a CurveStruct for each curve. Each of these curves has a linked List as a member. A linked list of pointers to PointStruct's. Imo this is all fine and dandy. But in linkedList.h I have: typedef PointStruct* dataType.Your linked list doesn't need to know anything about what's in a PointStruct in order to make this typedef. It only has to know that PointStruct is a struct. So instead of #including draw.h (so as to get the PointStruct definition), just use a forward declaration.However, if this actually is in C++, why aren't you using the standard library containers?   ;  I really dont care to put data structures in header files unless it is actually used outside of the program. My preferance is to put the struct in its own file along with its access routines and then call the access routines for most operations.Handling structures this way isolates them so that they can be altered easily with little to no effect on the rest of the program. Check out "Data Hiding" and "Data Coupling" for the in-depth reasons why.As a general practice, I put everything in the code file first and then "promote" it to the header file only when it becomes needed. This keeps the headers from containing stuff it shouldnt and also helps discourage the use of globals as well.   