如何修复 Google OR-Tools 中的 C++03 双尖括号模板语法错误
How do I fix a C++03 double angle bracket template syntax error in Google OR-Tools?
我正在尝试在OSX 10.11.3上构建一个Google OR-Tools示例
当我运行时:
g++ -I../include/ -L../lib -lortools -Wno-c++11-extensions nqueens2.cc
In file included from nqueens2.cc:25:
In file included from ../include/constraint_solver/constraint_solver.h:65:
In file included from ../include/base/hash.h:19:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/hash_map:212:5: warning: Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map> [-W#warnings]
# warning Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
^
In file included from nqueens2.cc:25:
In file included from ../include/constraint_solver/constraint_solver.h:65:
In file included from ../include/base/hash.h:20:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/hash_set:205:5: warning: Use of the header <ext/hash_set> is deprecated. Migrate to <unordered_set> [-W#warnings]
# warning Use of the header <ext/hash_set> is deprecated. Migrate to <unordered_set>
^
In file included from nqueens2.cc:25:
In file included from ../include/constraint_solver/constraint_solver.h:65:
../include/base/hash.h:184:31: error: expected expression
struct hash<std::array<T, N>> {
^
../include/base/hash.h:201:2: error: expected a type
};
^
2 warnings and 2 errors generated.
我认为问题是编译器在我希望它包含../include
时/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/hash_map
导致代码的错误来自以下../include/base/hash.h
(随Google OR-Tools一起提供(:
183 template <class T, std::size_t N>
184 struct hash<std::array<T, N>> {
185 public:
186 size_t operator()(const std::array<T, N>& t) const {
187 uint64 current = 71;
188 for (int index = 0; index < N; ++index) {
189 const T& elem = t[index];
190 const uint64 new_hash = hash<T>()(elem);
191 current = operations_research::Hash64NumWithSeed(current, new_hash);
192 }
193 return current;
194 }
195 // Less than operator for MSVC.
196 bool operator()(const std::array<T, N>& a, const std::array<T, N>& b) const {
197 return a < b;
198 }
199 static const size_t bucket_size = 4; // These are required by MSVC
200 static const size_t min_buckets = 8; // 4 and 8 are defaults.
201 };
202 #endif // STLPORT
203 } // namespace HASH_NAMESPACE
有什么想法吗?
旧的 g++ 编译器不允许模板声明连续两个>
,因为它显示为>>
运算符。因此,当您的代码查看结构声明时,它会看到一个{
大括号,而不是>>
运算符的表达式。这在后来的 c++ 标准中发生了变化,g++ 也相应地发生了变化。
C++03 编译器要求将>>
解释为右移运算符。这在 C++0x 标准中发生了变化。
我讨厌在源代码中指定包含目录路径,但它可能会解决问题:#include "../include/hash_map
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- C++语法头文件错误
- 如何摆脱错误c2061;语法错误 cout?
- 语法错误:D3DX10Core 中的标识符'TEXTMETRICA'
- 错误 C2146:语法错误:标识符"A1"之前缺少","
- main.cpp(11):错误 C2059:语法错误:"字符串"
- 我想获取点的属性,它报告错误 C3867:"point::output_x":非标准语法;使用"&"创建指向成员的指针
- RedisGraph 语法错误在偏移量 8 靠近"创建"
- 如何修复此 FSM C++语法错误 C3867
- 函数错误 C2059:语法错误:'>'不起作用
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- system() 返回错误语法错误:"("意外
- C++图形初始化错误(语法或丢失文件缺陷?
- 获取0:1(1):错误:语法错误,尝试编译着色器时意外$end
- Visual Studio错误:语法错误:标识符