A question about #include...
Hello.
I wanted to avoid using #include inside any of my header files, so that I can avoid dependency problems. I was under the impression that I could simply include all the headers I need in the corresponding .cpp file as long as the #include lines for those files appeared before I include the .h file in question.
For instance, suppose that I had Foo.h and Foo.cpp, and the header file has perhaps some STL vectors in it. I was under the impression that, instead of including <vector> in the .h file, I could include it in the .cpp file as long as the #include <vector> line appears before the #include Foo.h line, like this:
#include <vector>
#include Foo.h
However, when I try this with my program, it doesn't work. I get "missing type specifier" errors. What am I doing wrong?
Quote:Original post by CDProp
I wanted to avoid using #include inside any of my header files, so that I can avoid dependency problems. I was under the impression that I could simply include all the headers I need in the corresponding .cpp file as long as the #include lines for those files appeared before I include the .h file in question.
That's a BAD BAD BAD BAD BAD idea. This strategy requires someone writing a source file to remember not only the header files he needs, but the header files those header files require, and the header files required by THOSE header files, and so on. And if a header file gets new dependencies, you need to track down every source file which includes that header and make sure that the new dependency is added. And believe it or not, this won't help you avoid the dependency problems you spoke of. In fact, it will likely make them worse.
Follow this article.
Regardless, is my assumption about how the preprocessor directive works incorrect?
Edit: Thank you.
Edit: Thank you.
Quote:Original post by CDProp
Regardless, is my assumption about how the preprocessor directive works incorrect?
Nah, you've got it right. But still. BAD BAD BAD. Bad.
You're correct. Whenever the preprocessor encounters "#include <whatever>", it simply dumps the contents of whatever into that position.
So your example is exactly equivalent to simply including vector at the top of foo.h, except, of course, for style... a header should take care of its own dependencies.
So your example is exactly equivalent to simply including vector at the top of foo.h, except, of course, for style... a header should take care of its own dependencies.
Ok, I've taken them out. :P
I wonder why the compiler was confused about what a std::vector was, though, when I included <vector> in the source file before including the corresponding header.
*shrug*
Edit: I just realized that the header file was included in another source file that didn't have those includes it depended on before it. The other source file was likely compiled first, thus demonstrating your point. ROFL.
I wonder why the compiler was confused about what a std::vector was, though, when I included <vector> in the source file before including the corresponding header.
*shrug*
Edit: I just realized that the header file was included in another source file that didn't have those includes it depended on before it. The other source file was likely compiled first, thus demonstrating your point. ROFL.
An effective way to reduce header file dependencies in C++ is to use forward declarations. If a class is referenced in a header file via a pointer or reference or as a return value, then it is likely that you can use a forward declaration instead of including the header file. A forward declaration works something like this:
class Foo; // forward declaration class Bar { Foo GetFoo(); void SetFoo( Foo & foo ); };
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement