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

Started by
9 comments, last by mumpo 18 years, 8 months ago
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?
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.
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?
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.
i have no idea... do you know any toturial/article about it?
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.
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?
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.
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.
If they include each other, and you only want one copy, doesn't that in the end make them identical except maybe their order?

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement