C ++ 17在不使用自定义哈希映射的情况下自定义结构的前向声明

时间:2020-04-24 20:09:34

标签: c++ c++17

这一点让我很沮丧,但是我不确定我做错了什么。我的代码无法使用带有自定义哈希函数的哈希图进行编译,我认为编译器就可以了。

#include <iostream>
#include <queue>
#include <unordered_map>

struct Key;
struct THash;
struct TEqual;

class Cache {
private:
    uint64_t capacity;
    uint64_t time_ticker;
    std::unordered_map<Key, int32_t, THash, TEqual> map_keys;
    std::priority_queue<Key> lru_min_heap;

    void RehashPriorityQueue()
    {
        lru_min_heap = std::priority_queue<Key>(); 
        for (auto &&iter : map_keys) lru_min_heap.emplace(iter.first); 
    }

public:
    Cache(const uint64_t cache_size=1) : capacity(cache_size), time_ticker(0) {}
    ~Cache(){}
    void Set(int32_t key, int32_t value) {
        time_ticker++;

        auto _key = Key(key, time_ticker);
        auto find_key = map_keys.find(_key);

        if(capacity) capacity--;
        else
        {
            auto lru = lru_min_heap.top(); lru_min_heap.pop();
            auto find_key_lru = map_keys.find(lru);
            if(find_key_lru != map_keys.end()) map_keys.erase(find_key_lru);
        }

        if(find_key != map_keys.end()) 
        {
            map_keys.erase(find_key);
            RehashPriorityQueue();
        }

        map_keys.emplace(_key, value);
        lru_min_heap.emplace(_key);
    }
    int32_t Get(int32_t key) {
        time_ticker++;
        int ret = 0;

        auto _key = Key(key, time_ticker);
        auto find_key = map_keys.find(_key);
        if(find_key != map_keys.end()) {
            ret = find_key->second;
            map_keys.erase(find_key);
            RehashPriorityQueue();
            map_keys.emplace(_key, ret);
            lru_min_heap.emplace(_key);
        }
        else
        {
            std::cout<<"None ";
        }

        return ret;
    }
};

struct Key {
    int32_t id; 
    uint64_t weightage;
    Key(int32_t id, uint64_t weightage=1) : id(id), weightage(weightage) {};
    bool operator<(const Key &other) const { return weightage > other.weightage;}
    size_t operator()(const Key& tr) const {
        return std::hash<int32_t>()(tr.id);
    }
};

struct THash {
    size_t operator()(const Key& tr) const {
        return std::hash<int32_t>()(tr.id);
    }
};

struct TEqual {
    bool operator()(const Key& tr1, const Key& tr2) const {
        if (tr1.id == tr2.id) return true;
        return false;
    }
};

int main()
{
    Cache cache(2);
    cache.Set(3, 3);
    std::cout<<cache.Get(3)<<"\n";

    return 0;
}

编译器错误:

    In file included from /usr/include/c++/4.9/bits/hashtable.h:35:0,
                 from /usr/include/c++/4.9/unordered_map:47,
                 from 3:
/usr/include/c++/4.9/bits/hashtable_policy.h: In instantiation of 'struct std::__detail::__is_noexcept_hash<Key, THash>':
/usr/include/c++/4.9/type_traits:134:12:   required from 'struct std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> >'
/usr/include/c++/4.9/type_traits:145:38:   required from 'struct std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
/usr/include/c++/4.9/bits/unordered_map.h:100:66:   required from 'class std::unordered_map<Key, int, THash, TEqual>'
13:53:   required from here
/usr/include/c++/4.9/bits/hashtable_policy.h:85:33: error: no match for call to '(const THash) (const Key&)'
  noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
                                 ^
In file included from /usr/include/c++/4.9/bits/move.h:57:0,
                 from /usr/include/c++/4.9/bits/stl_pair.h:59,
                 from /usr/include/c++/4.9/bits/stl_algobase.h:64,
                 from /usr/include/c++/4.9/bits/char_traits.h:39,
                 from /usr/include/c++/4.9/ios:40,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/type_traits: In instantiation of 'struct std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >':
/usr/include/c++/4.9/bits/unordered_map.h:100:66:   required from 'class std::unordered_map<Key, int, THash, TEqual>'
13:53:   required from here
/usr/include/c++/4.9/type_traits:145:38: error: 'value' is not a member of 'std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> >'
     : public integral_constant<bool, !_Pp::value>
                                      ^
In file included from /usr/include/c++/4.9/unordered_map:48:0,
                 from 3:
/usr/include/c++/4.9/bits/unordered_map.h: In instantiation of 'class std::unordered_map<Key, int, THash, TEqual>':
13:53:   required from here
/usr/include/c++/4.9/bits/unordered_map.h:100:66: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc>  _Hashtable;
                                                                  ^
/usr/include/c++/4.9/bits/unordered_map.h:107:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::key_type key_type;
                                             ^
/usr/include/c++/4.9/bits/unordered_map.h:108:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::value_type value_type;
                                               ^
/usr/include/c++/4.9/bits/unordered_map.h:109:48: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::mapped_type mapped_type;
                                                ^
/usr/include/c++/4.9/bits/unordered_map.h:110:43: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::hasher hasher;
                                           ^
/usr/include/c++/4.9/bits/unordered_map.h:111:46: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::key_equal key_equal;
                                              ^
/usr/include/c++/4.9/bits/unordered_map.h:112:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::allocator_type allocator_type;
                                                   ^
/usr/include/c++/4.9/bits/unordered_map.h:117:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::pointer  pointer;
                                             ^
/usr/include/c++/4.9/bits/unordered_map.h:118:50: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::const_pointer const_pointer;
                                                  ^
/usr/include/c++/4.9/bits/unordered_map.h:119:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::reference  reference;
                                               ^
/usr/include/c++/4.9/bits/unordered_map.h:120:52: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::const_reference const_reference;
                                                    ^
/usr/include/c++/4.9/bits/unordered_map.h:121:46: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::iterator  iterator;
                                              ^
/usr/include/c++/4.9/bits/unordered_map.h:122:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::const_iterator const_iterator;
                                                   ^
/usr/include/c++/4.9/bits/unordered_map.h:123:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::local_iterator local_iterator;
                                                   ^
/usr/include/c++/4.9/bits/unordered_map.h:124:57: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::const_local_iterator const_local_iterator;
                                                         ^
/usr/include/c++/4.9/bits/unordered_map.h:125:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::size_type  size_type;
                                               ^
/usr/include/c++/4.9/bits/unordered_map.h:126:52: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       typedef typename _Hashtable::difference_type difference_type;
                                                    ^
/usr/include/c++/4.9/bits/unordered_map.h:242:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       operator=(initializer_list<value_type> __l)
       ^
/usr/include/c++/4.9/bits/unordered_map.h:340:2: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
  emplace(_Args&&... __args)
  ^
/usr/include/c++/4.9/bits/unordered_map.h:392:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       insert(const value_type& __x)
       ^
/usr/include/c++/4.9/bits/unordered_map.h:399:2: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
  insert(_Pair&& __x)
  ^
/usr/include/c++/4.9/bits/unordered_map.h:459:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       insert(initializer_list<value_type> __l)
       ^
/usr/include/c++/4.9/bits/unordered_map.h:604:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       equal_range(const key_type& __x)
       ^
/usr/include/c++/4.9/bits/unordered_map.h:608:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
       equal_range(const key_type& __x) const
       ^
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/4.9/bits/char_traits.h:39,
                 from /usr/include/c++/4.9/ios:40,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/bits/stl_pair.h: In instantiation of 'struct std::pair<const Key, int>':
19:28:   required from here
/usr/include/c++/4.9/bits/stl_pair.h:101:11: error: 'std::pair<_T1, _T2>::first' has incomplete type
       _T1 first;                 /// @c first is a copy of the first object
           ^
5:8: error: forward declaration of 'const struct Key'
 In member function 'void Cache::RehashPriorityQueue()':
19:28: error: no matching function for call to 'begin(std::unordered_map<Key, int, THash, TEqual>&)'
19:28: note: candidates are:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
                 from /usr/include/c++/4.9/string:52,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/initializer_list:89:5: note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
     begin(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/4.9/initializer_list:89:5: note:   template argument deduction/substitution failed:
19:28: note:   'std::unordered_map<Key, int, THash, TEqual>' is not derived from 'std::initializer_list<_Tp>'
In file included from /usr/include/c++/4.9/string:51:0,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/bits/range_access.h:87:5: note: template<class _Tp, long unsigned int _Nm> _Tp* std::begin(_Tp (&)[_Nm])
     begin(_Tp (&__arr)[_Nm])
     ^
/usr/include/c++/4.9/bits/range_access.h:87:5: note:   template argument deduction/substitution failed:
19:28: note:   mismatched types '_Tp [_Nm]' and 'std::unordered_map<Key, int, THash, TEqual>'
In file included from /usr/include/c++/4.9/string:51:0,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/bits/range_access.h:58:5: note: template<class _Container> decltype (__cont.begin()) std::begin(const _Container&)
     begin(const _Container& __cont) -> decltype(__cont.begin())
     ^
/usr/include/c++/4.9/bits/range_access.h:58:5: note:   template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.begin()) std::begin(const _Container&) [with _Container = std::unordered_map<Key, int, THash, TEqual>]':
19:28:   required from here
/usr/include/c++/4.9/bits/range_access.h:58:5: error: 'const class std::unordered_map<Key, int, THash, TEqual>' has no member named 'begin'
/usr/include/c++/4.9/bits/range_access.h:48:5: note: template<class _Container> decltype (__cont.begin()) std::begin(_Container&)
     begin(_Container& __cont) -> decltype(__cont.begin())
     ^
/usr/include/c++/4.9/bits/range_access.h:48:5: note:   template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.begin()) std::begin(_Container&) [with _Container = std::unordered_map<Key, int, THash, TEqual>]':
19:28:   required from here
/usr/include/c++/4.9/bits/range_access.h:48:5: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'begin'
19:28: error: no matching function for call to 'end(std::unordered_map<Key, int, THash, TEqual>&)'
19:28: note: candidates are:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
                 from /usr/include/c++/4.9/string:52,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/initializer_list:99:5: note: template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)
     end(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/4.9/initializer_list:99:5: note:   template argument deduction/substitution failed:
19:28: note:   'std::unordered_map<Key, int, THash, TEqual>' is not derived from 'std::initializer_list<_Tp>'
In file included from /usr/include/c++/4.9/string:51:0,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/bits/range_access.h:97:5: note: template<class _Tp, long unsigned int _Nm> _Tp* std::end(_Tp (&)[_Nm])
     end(_Tp (&__arr)[_Nm])
     ^
/usr/include/c++/4.9/bits/range_access.h:97:5: note:   template argument deduction/substitution failed:
19:28: note:   mismatched types '_Tp [_Nm]' and 'std::unordered_map<Key, int, THash, TEqual>'
In file included from /usr/include/c++/4.9/string:51:0,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/bits/range_access.h:78:5: note: template<class _Container> decltype (__cont.end()) std::end(const _Container&)
     end(const _Container& __cont) -> decltype(__cont.end())
     ^
/usr/include/c++/4.9/bits/range_access.h:78:5: note:   template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.end()) std::end(const _Container&) [with _Container = std::unordered_map<Key, int, THash, TEqual>]':
19:28:   required from here
/usr/include/c++/4.9/bits/range_access.h:78:5: error: 'const class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
/usr/include/c++/4.9/bits/range_access.h:68:5: note: template<class _Container> decltype (__cont.end()) std::end(_Container&)
     end(_Container& __cont) -> decltype(__cont.end())
     ^
/usr/include/c++/4.9/bits/range_access.h:68:5: note:   template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.end()) std::end(_Container&) [with _Container = std::unordered_map<Key, int, THash, TEqual>]':
19:28:   required from here
/usr/include/c++/4.9/bits/range_access.h:68:5: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
 In constructor 'Cache::Cache(uint64_t)':
23:77: error: no matching function for call to 'std::unordered_map<Key, int, THash, TEqual>::unordered_map()'
23:77: note: candidates are:
In file included from /usr/include/c++/4.9/unordered_map:48:0,
                 from 3:
/usr/include/c++/4.9/bits/unordered_map.h:172:7: note: std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&&) [with _Key = Key; _Tp = int; _Hash = THash; _Pred = TEqual; _Alloc = std::allocator<std::pair<const Key, int> >]
       unordered_map(unordered_map&&) = default;
       ^
/usr/include/c++/4.9/bits/unordered_map.h:172:7: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/4.9/bits/unordered_map.h:169:7: note: std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(const std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&) [with _Key = Key; _Tp = int; _Hash = THash; _Pred = TEqual; _Alloc = std::allocator<std::pair<const Key, int> >]
       unordered_map(const unordered_map&) = default;
       ^
/usr/include/c++/4.9/bits/unordered_map.h:169:7: note:   candidate expects 1 argument, 0 provided
 In member function 'void Cache::Set(int32_t, int32_t)':
28:41: error: invalid use of incomplete type 'struct Key'
5:8: error: forward declaration of 'struct Key'
29:34: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'find'
34:41: error: 'Key lru' has incomplete type
35:42: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'find'
36:41: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
36:57: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'erase'
39:33: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
41:22: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'erase'
45:18: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'emplace'
 In member function 'int32_t Cache::Get(int32_t)':
52:41: error: invalid use of incomplete type 'struct Key'
5:8: error: forward declaration of 'struct Key'
53:34: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'find'
54:33: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
56:22: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'erase'
58:22: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'emplace'

0 个答案:

没有答案
相关问题