我遇到了一个我无法理解的编译时错误。我尝试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 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 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"