简体   繁体   中英

c++ Include files not carried over

I have three files:

  • domain.h defines some plain structs. one of the fields is a boost::optional
  • convert.h defines template specialization that convert the plain structs to some other type.
  • lib.cpp that uses domain.h and converts.h

So, the includes are:

//domain.h
#include <boost/optional>
//convert.h 
#include "domain.h"
#include <boost/optional>
//lib.cpp  --- version 1 does not work
#include "domain.h"
#include "convert.h"

The compiler(VS2010) gave a uninformative error message. I tried adding the boost optional include:

//lib.cpp  --- version 2 works
#include <boost/optional>
#include "domain.h"
#include "convert.h"

How come the boost/optional header include wasn't carried over from domain.h and/or convert.h to lib.cpp?

EDIT:

The compiler complained it could find a template specialization for boost::optional.

I found a problem. there's another file which defines the conversion specialization for boost::optional, convert-boost-optional.h

If I change convert.h to

#include "domain.h"
#include <convert-boost-optional.h>  

then version 1 works too. What puzzles me is how come version 2 compiles without including convert-boost-optional.h anywhere?

EDIT: I got myself confused. I actually included <convert-boost-optional.h> in lib.cpp in version 2. Everything makes sense now. Sorry!

I'm supposing the hearder doesn't have header guards, so you could place them in your code instead:

#ifndef _BOOST_OPTIONAL_H_
#define _BOOST_OPTIONAL_H_
#include <boost/optional>
#endif

If you place this in both occasoins where you include them, you should no longer experience it.


But, you might wonder: Do you actually need the include to be in the header file?

Even if you need something from the header file in your header file, you might not need to include the header file. Learn about forward declarations and also about when you can't use them .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM