我遇到了一个编译时错误,但我无法理解。我试图在我的代码中使用
编译器报错的位置在Boost头文件
编译器输出只显示了这些消息:
这显然不是 Boost 库的问题。但我该如何找出我的类或项目设置有什么问题呢?
附言:即使我在项目中使用这些最原始的头文件和源文件,我也能重现这种行为。
头文件 Test.h:
boost::optional
,但是一旦我包含了boost/optional.hpp
,我就无法再构建我的项目了。如果我注释掉这个包含语句,它就能工作。我甚至还没有在我的代码中实际使用 boost::optional
,只是在类头文件中包含了这个语句(请参见下面的完整头文件)。编译器错误是C2143 语法错误:'<'之前缺少','
,这发生在另一个 Boost 头文件 boost/utility/compare_pointees.hpp
中(请参见下面的 GitHub 链接)。我已经在同一个项目中成功使用了来自 Boost 的其他东西,如 boost::filesystem::path
,因此我的 Boost 分发本身应该没有问题。
这是我的环境:Microsoft Visual Studio Professional 2015 版本 14.0.25431.01 更新 3
和 boost 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行。您可以在GitHub上查看此文件的完整内容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"
/std:c++latest
编译选项?我知道这个选项会移除一些将在c++17中移除的内容,因此可能Boost(或这个特定的Boost库)还没有适应这种变化。如果你在使用它,可以尝试使用/std:c++14
选项,看看能否解决问题。 - llonesmiz