Jump to content
  • Advertisement
Sign in to follow this  
CDProp

A question about #include...

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

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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
Regardless, is my assumption about how the preprocessor directive works incorrect?

Edit: Thank you.

Share this post


Link to post
Share on other sites
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!