2

我遇到了一个我无法理解的编译时错误。我尝试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 header第 36 行。你可以在 GitHub 上的https://github.com/boostorg/utility/blob/boost-1.62.0boost/utility/compare_pointees.hpp下查看该文件的完整内容/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 库的问题。但是我如何才能弄清楚我的课程或项目设置有什么问题?

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

头文件Test.h

#pragma once

#include <boost/optional.hpp>

源文件Test.cpp

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

3 回答 3

4

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

于 2016-10-20T16:05:43.327 回答
3

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

于 2016-12-30T12:31:08.050 回答
1

就我而言,我在 Force 包含文件(C++->Advanced)中有一个#define new DEBUG_NEW。我通过添加#undef new berore 来修复它#include's 然后#define new DEBUG_NEW 之后。

于 2018-09-13T11:37:21.437 回答