【问题标题】:c++: stl hash compilation issuesc++: stl hash编译问题
【发布时间】:2012-03-24 02:41:02
【问题描述】:

以下示例无法为我编译:

#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

【问题讨论】:

  • 尝试使用-std=c++0x 标志,我认为std::hash 是C++11 的一部分,不是吗?
  • 不,cc1plus: error: unrecognized command line option "-std=c++0x"
  • 我想我明白了(见下文)似乎是特定于 mac 的命名空间问题,感谢您的帮助 :)

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


【解决方案1】:

找到答案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';
}

【讨论】:

  • 如果你使用macports,你可以在你的mac上安装gcc 4.6。
猜你喜欢
  • 2011-01-10
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多