A question about #include...

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

Recommended Posts

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?

Share on other sites
Quote:
 Original post by CDPropI 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.

Share on other sites
Regardless, is my assumption about how the preprocessor directive works incorrect?

Edit: Thank you.

Share on other sites
Quote:
 Original post by CDPropRegardless, is my assumption about how the preprocessor directive works incorrect?

Share on other sites
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.

Share on other sites
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.

Share on other sites
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 );    };

Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• Forum Statistics

• Total Topics
628760
• Total Posts
2984542

• 12
• 25
• 12
• 10
• 17