包含 boost/Optional.hpp 时出现 C2143 语法错误

2024-02-07

我遇到了一个我无法理解的编译时错误。我尝试使用boost::optional在我的代码中,一旦我包含boost/optional.hpp我无法再构建我的项目了。如果我注释掉这个 include 语句,它就会起作用。我什至没有任何实际用途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 3 and boost 1.62.0。我也使用第三方库C++ REST SDK https://github.com/Microsoft/cpprestsdk,其他一切都是 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 行。您可以在 GitHub 上查看该文件的完整内容https://github.com/boostorg/utility/blob/boost-1.62.0/include/boost/utility/compare_pointees.hpp https://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"

由于有价值的提示,我可以找出原因jv_ https://stackoverflow.com/users/2417774/jv。我打开了编译器开关/std:c++latest在我的项目设置中能够使用 C++17嵌套命名空间定义 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4230.html特征。激活此开关会删除一些已弃用的语言功能,特别是std::binary_function,它仍在当前的 Boost 发行版 (1.62.0) 中使用,因此会产生编译器错误。最后我决定把开关去掉/std:c++latest(并使用普通方式声明我的名称空间)这解决了问题。谢谢大家对我的帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

包含 boost/Optional.hpp 时出现 C2143 语法错误 的相关文章

随机推荐