2

我收到一个错误

error: use of undeclared identifier '__stl_hash_string'
                    { return __stl_hash_string( __s.c_str() ); }

在 Mac OS 10.8 上使用 Xcode 4.6.1 编译时。

/ ------下面的代码片段---- /

#ifdef __cplusplus
    namespace __gnu_cxx
    {
            template<>
            struct hash<std::string>
            {
                    size_t operator()(const std::string& __s) const
                    { return __stl_hash_string( __s.c_str() ); } 
            };
    }
#endif

/ -------------------------------------- / 此代码在 Xcode 3.5 上运行良好Mac OSX 10.7 和 10.6。

我搜索了该__stl_hash_string方法,发现它存在于文件夹中 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ext/hash_fun.h

但是,当我编写示例应用程序以查看是否可以包含此标头时,它失败了。

#include < cstddef >
#include < ext/hash_fun.h >

在第二行给了我一个错误,说不能包含这个标题。我不确定这种方法是否在新环境中被弃用,如果它被弃用,那么替代方法是什么。我请求您帮助解决这个问题。

4

3 回答 3

2

看起来像在 libstdc++ 中定义的这个函数。这个库在 Xcode 3.5 中是默认的,现在 Xcode 默认使用 libc++。但是您仍然可以在C++ Standard Library构建设置中将其切换到 libstdc++。
但是如果你可以在你的项目中使用 C++11,我建议你使用标准的std::hash函数作为字符串,而不是依赖于内部的 std 函数。

于 2013-04-29T05:59:29.703 回答
1
I have modified the code again so that there won't be any issues for any other clients using this header for the hash functionality.

// ---------------------------------------------------------------------------
//  • hash function support
// ---------------------------------------------------------------------------
//
#ifdef _LIBCPP_VERSION
    template<>
    struct hash<std::string>
    {
        size_t operator()(const std::string& __s) const
        {
            std::hash<std::string> hash_fn;
            return hash_fn(__s);
        }
    };
#elif __cplusplus

    namespace __gnu_cxx
    {
        template<>
        struct hash<std::string>
        {
            size_t operator()(const std::string& __s) const
            { return __stl_hash_string( __s.c_str() ); }
        };
    }
#endif
于 2013-04-30T15:27:49.827 回答
1

好吧,当我们使用 libc++ 时,我已经修改了我的头文件不使用这个哈希支持定义

/

/ ---------------------------------------------------------------------------
//      • hash function support
// ---------------------------------------------------------------------------
#ifdef _LIBCPP_VERSION
        /*std::hash available in libc++ so no hash support required*/
#elif __cplusplus
        namespace __gnu_cxx
        {
                template<>
                struct hash<std::string>
                {
                        size_t operator()(const std::string& __s) const
                        { return __stl_hash_string( __s.c_str() ); }
                };
        }
#endif

感谢科迪的回答。现在它编译得很好。但是添加 '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/‌​SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ 无济于事,因为此代码由许多产品。所以不能放置绝对标题路径。

于 2013-04-30T13:09:20.930 回答