Sign in to follow this  
CDProp

A question about #include...

Recommended Posts

CDProp    1451
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 this post


Link to post
Share on other sites
Sneftel    1788
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.

Share this post


Link to post
Share on other sites
Sneftel    1788
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.

Share this post


Link to post
Share on other sites
smitty1276    560
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 this post


Link to post
Share on other sites
CDProp    1451
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 this post


Link to post
Share on other sites
JohnBolton    1372
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 this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this