Jump to content
  • Advertisement
Sign in to follow this  
urisoft

spliting code to files: #include cycles...how?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm trying to split my code in to several files. i read an article here and got started ok but i have a problam. i have two files and i want each one of them to include the other. like: ///file 1 #include "file2.h" ///code... ///file 2 #include "file1.h" ///code.... but whenever i do that i get errors. is there a way to do it right?

Share this post


Link to post
Share on other sites
Advertisement
Such recursive header relationships are normally solved by forward declarations of classes. Seeing as you don't say exactly what code you have tried nor the exact erros you have, the most I can suggest is to read this article.

Share this post


Link to post
Share on other sites
i'm sorry i'll be more specific.

one file has all the types and global variables and the second file has a class and all it's function

i read the article before and tried the forward declaration thing and it compiled fine, but i couldn't use the class. i get this error:
"aggregate 'Cman man' has incomplete type and cannot be defined
storage size of man unknown"

Cman is the class defined on one file and man is the var.

any idea?

Share this post


Link to post
Share on other sites
Did you throw in a #pragma once or #ifndef/#define/#endif block? Those structures, properly applied, solve 99% of inclusion problems. Of course, that other 1% can be quite difficult to get rid of. Anyways, reply if you have no idea what I'm talking about.

Share this post


Link to post
Share on other sites
Quote:
Original post by urisoft
one file has all the types and global variables and the second file has a class and all it's function
Why does the first file, which has types and global variables, need the class and its functions?

You need to refactor.

Share this post


Link to post
Share on other sites
because i want to make a global variable of this class.

and even if it's not neccecary in this case, i want to know how to use this kind of stuff for other situations
and what do you meen by refactor?

Share this post


Link to post
Share on other sites
Quote:
Original post by urisoft
because i want to make a global variable of this class.
But you then want to introduce that global variable in the scope of the class declaration?

Clearly, you have some entities together that don't have the same project scope. At least one of your headers needs to be split, establishing a simpler hierarchy. I'd suggest separating type declarations from global declarations/definitions.

Quote:
and what do you meen by refactor?
Refactoring.

Share this post


Link to post
Share on other sites
Quote:
Original post by urisoft
i have no idea... do you know any toturial/article about it?

No need; it is pretty simple. This is no good:

// file1.h
#include "file2.h"

// some code

// file2.h
#include "file1.h"

// some other code


File 1 includes file 2 which includes file 1 which includes file 2 which includes file 1, to infinity. The compiler doesn't like that. Instead, you have to add some code to stop each file from being included more than once:

// file1.h
#ifndef FILE_ONE_INCLUDED
#define FILE_ONE_INCLUDED
#include "file2.h"

// some code
#endif

// file2.h
#ifndef FILE_TWO_INCLUDED
#define FILE_TWO_INCLUDED
#include "file1.h"

// some other code
#endif


Now file 1 defines FILE_ONE_INCLUDED, then includes file 2, which includes file 1, but it stops there because FILE_ONE_INCLUDED is defined this time, so the code of file 1 is skipped. If you are using Visual Studio, you can simplify it by using #pragma once, which is a non-standard shortcut that does the same thing as the #ifndef/#define/#endif block:

// file1.h
#pragma once
#include "file2.h"

// some code

// file2.h
#pragma once
#include "file1.h"

// some other code


All of those commands beginning with # are preprocessor directives, meaning that they effect the way the compiler reads the code, not how the code actually runs. (Yes, that is an oversimplification, but it is close enough for now.) Learn a little about them if you are going to start messing with multi-file projects.

Share this post


Link to post
Share on other sites
If they include each other, and you only want one copy, doesn't that in the end make them identical except maybe their order?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!