c++:STL哈希编译问题

标签 c++ macos stl compiler-errors g++

以下示例不会为我编译:

#include <iostream>
#include <functional>
#include <string>

int main()
{
  std::string str = "Meet the new boss...";
  std::hash<std::string> hash_fn;
  size_t str_hash = hash_fn(str);

  std::cout << str_hash << '\n';
}

我从 g++ 得到以下输出

> g++ -o test test.cpp 
test.cpp: In function ‘int main()’:
test.cpp:8: error: ‘hash’ is not a member of ‘std’
test.cpp:8: error: expected primary-expression before ‘>’ token
test.cpp:8: error: ‘hash_fn’ was not declared in this scope

我是否缺少编译标志或其他什么?

相关信息:

> g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

> uname -a
Darwin ############# 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; ro

最佳答案

找到答案 here

此代码编译:

#include <iostream>
#include <tr1/functional>
#include <string>

int main()
{
  double d = 3;
  std::tr1::hash<double> hash_fn;
  size_t str_hash = hash_fn(d);

  std::cout << str_hash << '\n';
}

关于c++:STL哈希编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9848692/

相关文章:

c++ - 使用 lambda 函数排序,然后在 C++ 中排序

c++ - 可以删除抽象类而不是子类吗?

macos - Cython:prange 正在重复而不是并行化

windows - Vagrant shell provisioner 在 Mac 主机上失败但在 Windows 主机上工作

c++ - Typedef——为什么这个 C++ 代码有效?

c++ - 用于自定义容器的 STL 兼容迭代器

c++ - 为什么时钟()被认为是坏的?

包含 return 的 C++ 宏表达式(就像 Rust 的 try!)

python - 如何通过 Python 在 Mac 中获取桌面分辨率?

c++ - 是否可以让 C++ 方法在不重载方法的情况下接受 const char* 和 const wchar_t* 作为参数?