#### Archived

This topic is now archived and is closed to further replies.

# Circular Depandancies

This topic is 5332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I could not get rid of them on my exam and I cant get rid of them now. I am having endless problems with circular dependancies with .h files including other .h files that have already been included if you catch me? The result being things such as global variables being declared twice. A friend of mine said you have to use some #ifdef or something? Can someone explain this to me and how to use it (assuming my friend was right)? Thanks as always.

##### Share on other sites
Run a search for "inclusion gaurds".

But, how it works is like this:

#ifndef MY_BoxClass_H
#define MY_BoxClass_H

... Put includes here ...

... Put classes here ...

#endif

Basically, this keeps the compiler from reading and defining the same code multiple times.

Also, do not define global variables in a header like such:

int g_Handle;

handle.h

extern int g_Handle;

handle.cpp

int g_Handle;

That way it is not defined multiple times, but other source file know of it''s existance...

##### Share on other sites
Thanks, but just one thing...what goes where the MY_BoxClass_H is? I dont understand what this is...

##### Share on other sites
That''s a FAQ.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

##### Share on other sites
Yeah I just found that, thanks anyway

I cant believe we were not taught inclusion guards this semester in software development principals...would have saved me alot of trouble.

Anyhow, thanks again.

##### Share on other sites
Watch out for class circular dependency as well (for example, when two classes have members that are of the other class).

Inclusion guards don't entirely prevent them. You need an empty declaration of a class in order for it to work.

class A;
class B
{
A data;
};

class A
{
int value;
B member;
};

I think that works.

[edited by - Waverider on January 12, 2004 12:53:11 PM]

##### Share on other sites
You can also use '#pragma once' if you only want a file included once during compilation (if your compiler supports it).

[edited by - arm on January 12, 2004 2:36:56 PM]

##### Share on other sites
Hmm for some reason I still cant get rid of some of them. This is what I have in my OpenGL.h file (included in 2 .c files). hDC, hRC, hWnd and hInstance are still causing problems. Actually ifndef didnt seem to solve anything, declaring keys, active and fullscreen, then defining them in the .c file solved the problem with them, but the others are no good. The functions i never had a problem with.
#ifndef INC_OPENGL_H#define INC_OPENGL_H#include <windows.h>#include <gl\gl.h>#include <gl\glu.h>			//#include <gl\glaux.h>extern bool	keys[256];extern bool	active;extern bool	fullscreen;extern HDC        hDC;extern HGLRC	  hRC;extern HWND	  hWnd;extern HINSTANCE  hInstance;GLvoid ReSizeGLScene(GLsizei width, GLsizei height);int InitGL(GLvoid);int DrawGLScene(GLvoid);GLvoid KillGLWindow(GLvoid);BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag);LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);#endif /*INC_OPENGL_H*/

The problem with the global variables was that i was defining them in the header...not just declaring (i am a bit rusty). However, the other 4 globals are not working, still causing the double defining.

I was also taught that extern actually does nothing as all functions and global variables are globals in C anyway, unless declared static, making them local to the source file...

##### Share on other sites
If you post the exact error messages, it will be easier to help solve your specific error.

##### Share on other sites
quote:
Original post by Waverider
Watch out for class circular dependency as well (for example, when two classes have members that are of the other class).

Inclusion guards don''t entirely prevent them. You need an empty declaration of a class in order for it to work.

class A;
class B
{
A data;
};

class A
{
int value;
B member;
};

I think that works.

[edited by - Waverider on January 12, 2004 12:53:11 PM]

That doesn''t work. This works.

class Bar;

class Foo
{
Bar * pBar;
};

class Bar
{
Foo * pFoo;
};

You have to use pointers (at least one has to be a pointer) or it won''t compile. It''s not even logical if you really think about it.

-----------
VenDrake

"My stupid jar is full. I can''t talk to you anymore."

1. 1
2. 2
3. 3
4. 4
frob
12
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632172
• Total Posts
3004566

×