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 OS X 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 >

2行目に、このヘッダーを含めることができないというエラーが表示されました。この方法が新しい環境で非推奨かどうかはわかりません。非推奨の場合、代替方法は何ですか。この問題を解決するためにあなたの助けを求めます。

4

3 に答える 3

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 に答える
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 に答える