0

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!

4

1 回答 1

0

我假设听者没有标题保护,所以你可以将它们放在你的代码中:

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

如果你把它放在你包含它们的两个场合中,你应该不再体验它。


但是,您可能想知道:您真的需要包含在头文件中吗?

即使您需要头文件中的头文件中的某些内容,您也可能不需要包含头文件。了解前向声明以及何时不能使用它们

于 2012-11-16T17:24:35.517 回答