【问题标题】:C2143 syntax error when including boost/optional.hpp包含 boost/optional.hpp 时出现 C2143 语法错误
【发布时间】:2017-02-27 17:58:03
【问题描述】:

我遇到了一个我无法理解的编译时错误。我尝试在我的代码中使用boost::optional,一旦我包含boost/optional.hpp,我就无法再构建我的项目了。如果我将此包含声明注释掉,它会起作用。我的代码中还没有boost::optional 的任何实际用法,只是类头中的include 语句(参见下面的完整头)。编译器错误是 C2143 syntax error: missing ',' before '<',它发生在另一个 Boost 标头 boost/utility/compare_pointees.hpp 中(请参阅下面的 GitHub 链接)。我还成功地在同一个项目中使用了来自 Boost 的其他东西,例如 boost::filesystem::path,所以我的 Boost 发行版应该没有问题。

这是我的环境:Microsoft Visual Studio Professional 2015 Version 14.0.25431.01 Update 3boost 1.62.0。我也用第三方库C++ REST SDK,其他都是C++标准库的东西。

我的标题看起来像这样。我想添加一个以boost::optional<size_t> 作为返回类型的新方法。

#pragma once

#include <boost/optional.hpp>   // <==== ERROR

// C++ REST SDK
#define _TURN_OFF_PLATFORM_STRING
#include <cpprest/http_listener.h>
#include <cpprest/http_msg.h>

namespace SANDBOX::REST
{
   class HttpGetHandler
   {
   public:
       virtual void HandleHttpGetRequest(web::http::http_request request) = 0;
   };
}

编译器报错的地方在Boost头boost/utility/compare_pointees.hpp第36行。可以在GitHubhttps://github.com/boostorg/utility/blob/boost-1.62.0/include/boost/utility/compare_pointees.hpp下查看这个文件的完整内容

编译器输出仅显示以下消息:

1>D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(36): error C2143: syntax error: missing ',' before '<'
1>  D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(40): note: see reference to class template instantiation 'boost::equal_pointees_t<OptionalPointee>' being compiled
1>D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(59): error C2143: syntax error: missing ',' before '<'
1>  D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(63): note: see reference to class template instantiation 'boost::less_pointees_t<OptionalPointee>' being compiled
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

这肯定不是 Boost 库的问题。但是我如何才能弄清楚我的课程或项目设置有什么问题?

附:即使我在项目中使用这些最原始的头文件和源文件,我也可以重现该行为:

头文件Test.h:

#pragma once

#include <boost/optional.hpp>

源文件Test.cpp:

#include "../include/Test.h"

【问题讨论】:

  • 请将您的问题edit发送至minimal reproducible exampleSSCCE (Short, Self Contained, Correct Example)。同时发布文本,而不是图片。
  • 向我们显示触发错误的确切行,并请编辑您的问题以将代码包含为文本,而不是屏幕截图。此外,std::binary_function 已被弃用。
  • 除了“停止发布图片,我不能将它们复制/粘贴到repro”之外,关于“所以我的Boost发行版应该没有问题。”:就像可能,指定您使用的 boost 版本不会有什么坏处。
  • @SingerOfTheFall 这个弃用的用法来自 Boost 标头,而不是来自我的代码。
  • 您是否有可能使用/std:c++latest?我知道这个开关删除了一些将在 c++17 中删除的东西,并且 Boost(或这个特定的 Boost 库)可能还没有适应这种变化。如果您正在使用它,您应该尝试/std:c++14 看看它是否适合您。

标签: c++ boost visual-studio-2015 optional boost-optional


【解决方案1】:

在我的情况下,我在 Force 包含文件 (C++->Advanced) 中有一个 #define new DEBUG_NEW。 我通过添加 #undef new 来修复它,在 boost #include's 之后添加 #define new DEBUG_NEW。

【讨论】:

    【解决方案2】:

    在 boost 1.63.0 中修复的问题。它不再使用在 C++17 中删除的std::binary_function

    【讨论】:

    • 是的,我绝对可以确认这个说法。使用boost 1.63.0,我可以毫无问题地使用/std:c++latest 开关和boost/optional.hpp
    • 不幸的是,std::unary_function 在 boost\algorithm\string\detail\util.hpp(boost 版本 1.64)中还有另一种用法。 :-(
    • 还有std::unary_function的用法和boost\icl\type_traits\predicate.hpp中的std::binary_function的用法。 (版本 1.65)
    【解决方案3】:

    由于jv_ 的宝贵提示,我可以找出原因。我在我的项目设置中打开了编译器开关 /std:c++latest 以便能够使用 C++17 nested namespace definition 功能。激活此开关会删除一些已弃用的语言功能,特别是 std::binary_function,它仍在当前的 Boost 发行版 (1.62.0) 中使用,因此会产生编译器错误。最后,我决定删除开关/std:c++latest(并使用普通的方式来声明我的命名空间),这解决了这个问题。谢谢大家帮助我。

    【讨论】:

    • 我仍然能够使用 /std:c++latest 进行编译,只需在我的 compare_pointees.hpp 副本中删除 equal_pointees_tless_pointees_t 结构模板中的 std::binary_function 的继承。如果您真的想在 Boost 自己删除已弃用的用法之前使用 C++17 功能,这可能是一个很好的解决方法。
    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2014-01-06
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 2012-07-26
    相关资源
    最近更新 更多