Public Group

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

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

## 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 on other sites
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 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 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 on other sites
i have no idea... do you know any toturial/article about it?

##### Share on other sites
Quote:
 Original post by urisoftone 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 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 on other sites
Quote:
 Original post by urisoftbecause 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 on other sites
Quote:
 Original post by urisofti 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 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?

• 33
• 15
• 23
• 10
• 19