为什么我在iterator_facade中得到编译错误取决于我把我的Boost头包含在哪里

Why do I get compilation errors in iterator_facade depending on where I put my Boost header includes?

本文关键字:我的 取决于 Boost 在哪里 包含 错误 iterator facade 为什么 编译      更新时间:2023-10-16

我正在尝试将Boost合并到我现有的中型项目中。我有预编译的头文件,stdafx.h,在那里我保留了所有的c++标准库#include指令(加上一些Windows头文件),以及许多.cpp文件,其中包括stdafx.h,其中A.cpp。它在第一行包含stdafx.h,然后仅包含该文件所需的一些其他头文件。我想在A.cpp中使用boost::program_options,所以我把它添加到stdafx.h之后的包含列表中,所以A.cpp的顶部看起来像这样:

#include "stdafx.h"
#include <boost/program_options.hpp>
// more #includes

然后我开始编译这个项目,但是遇到了以下错误:

Severity    Code    Description Project File    Line
Error   C2976   'boost::iterators::detail::postfix_increment_result': too few template arguments    testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  789
Error   C1903   unable to recover from previous error(s); stopping compilation  testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  790
Error   C2143   syntax error: missing ';' before '++'   testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  790
Error   C2059   syntax error: ','   testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  534
Error   C2059   syntax error: ','   testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  541
Error   C2059   syntax error: ','   testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  600
Error   C2059   syntax error: ','   testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  606
Error   C2059   syntax error: ','   testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  789
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  790
Error   C2433   'type': 'inline' not permitted on data declarations testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  790
Error   C2888   'if_<T1,T2,T3>::type type': symbol cannot be defined within namespace 'iterators'   testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  790
Error   C2976   'boost::iterators::iterator_facade': too few template arguments testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  534
Error   C2976   'boost::iterators::iterator_facade': too few template arguments testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  541
Error   C2976   'boost::iterators::iterator_facade': too few template arguments testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  600
Error   C2976   'boost::iterators::iterator_facade': too few template arguments testProject C:boost_1_59_0boostiteratoriterator_facade.hpp  606

我做了一些实验,发现如果我把

#include <boost/program_options.hpp>

stdafx.h非常末尾——最后一行——一切都编译得很好,但是如果我试图把它放在A.cpp的任何地方,我得到了上面的错误。我真的不明白这是怎么可能的;难道预处理器不只是执行无意识的插入,在这种情况下,在预处理器完成其工作后,应该将相同的输出提供给编译器?原因是什么呢?我已经搜索过了,但我只发现了那些实际上想要使用iterator_facade的人的帖子,并且在他们的代码中得到错误,而不是在Boost头中。

我的配置:VS 2015 on Windows 8.1, Boost 1.59.0, x86项目,使用/MT构建

UPDATE:如果我在A.cppstdafx.h中都包含该头文件,它也可以工作。

UPDATE2:有趣的是,这是编译失败后我在输出窗口中的内容:

1>  INTERNAL COMPILER ERROR in 'C:Program Files (x86)Microsoft Visual Studio 14.0VCbinCL.exe'
1>      Please choose the Technical Support command on the Visual C++
1>      Help menu, or open the Technical Support help file for more information

最可能的解释是,您的一个头文件缺少包含保护(或包含保护有一个打字错误),而您实际上包含了一些boost头文件两次。(不是所有的boost头文件都单独包含了守卫。你应该包括的那些,但其中一些只是没有"出于效率的原因"我被告知。

或者,您在某个命名空间中包含boost,这是它无法真正保护自己的事情。

这个错误'boost::iterators::detail::postfix_increment_result': too few template arguments正是我所期望的,如果它是一些模板定义和编译器第二次遇到定义。