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