fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_set>
  4.  
  5. struct Movie{
  6. std::string name;
  7. int year;
  8.  
  9. Movie(std::string n, int y): name(std::move(n)), year(y)
  10. {
  11. }
  12.  
  13. bool operator ==(const Movie &m) const
  14. {
  15. return year == m.year && name == m.name;
  16. };
  17. };
  18.  
  19.  
  20. namespace std
  21. {
  22. template <>
  23. struct hash<Movie>
  24. {
  25. size_t operator()(const Movie& movie) const
  26. {
  27. return hash<std::string>{}(movie.name + to_string(movie.year));
  28. }
  29. };
  30. }
  31. ////////////////////
  32.  
  33. struct ActorNode
  34. {
  35. std::string name;
  36. std::unordered_set<Movie> movies;
  37.  
  38. ActorNode(std::string n) : name(std::move(n))
  39. {
  40. }
  41.  
  42. bool operator ==(const ActorNode &other) const
  43. {
  44. return name == other.name;
  45. }
  46. };
  47.  
  48.  
  49. namespace std
  50. {
  51. template <>
  52. struct hash<ActorNode>
  53. {
  54. size_t operator()(const ActorNode& actor) const
  55. {
  56. return hash<std::string>{}(actor.name);
  57. }
  58. };
  59. }
  60. ////////////////////
  61.  
  62.  
  63. int main()
  64. {
  65. std::unordered_set<ActorNode> actors;
  66. actors.emplace("Gene Wilder");
  67.  
  68. auto itr = actors.find(ActorNode("Gene Wilder"));
  69. if (itr != actors.end())
  70. {
  71. // error, no such method 'insert'
  72. itr->movies.insert(Movie("Stir Crazy", 1981));
  73. }
  74. }
  75.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:72:53: error: no matching function for call to ‘std::unordered_set<Movie>::insert(Movie) const’
         itr->movies.insert(Movie("Stir Crazy", 1981));
                                                     ^
In file included from /usr/include/c++/6/unordered_set:48:0,
                 from prog.cpp:3:
/usr/include/c++/6/bits/unordered_set.h:412:7: note: candidate: std::pair<typename std::_Hashtable<_Value, _Value, _Alloc, std::__detail::_Identity, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Tp, _Hash> > >::value, true, true> >::iterator, bool> std::unordered_set<_Value, _Hash, _Pred, _Alloc>::insert(const value_type&) [with _Value = Movie; _Hash = std::hash<Movie>; _Pred = std::equal_to<Movie>; _Alloc = std::allocator<Movie>; typename std::_Hashtable<_Value, _Value, _Alloc, std::__detail::_Identity, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Tp, _Hash> > >::value, true, true> >::iterator = std::__detail::_Node_iterator<Movie, true, true>; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::value_type = Movie] <near match>
       insert(const value_type& __x)
       ^~~~~~
/usr/include/c++/6/bits/unordered_set.h:412:7: note:   passing ‘const std::unordered_set<Movie>*’ as ‘this’ argument discards qualifiers
/usr/include/c++/6/bits/unordered_set.h:416:7: note: candidate: std::pair<typename std::_Hashtable<_Value, _Value, _Alloc, std::__detail::_Identity, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Tp, _Hash> > >::value, true, true> >::iterator, bool> std::unordered_set<_Value, _Hash, _Pred, _Alloc>::insert(std::unordered_set<_Value, _Hash, _Pred, _Alloc>::value_type&&) [with _Value = Movie; _Hash = std::hash<Movie>; _Pred = std::equal_to<Movie>; _Alloc = std::allocator<Movie>; typename std::_Hashtable<_Value, _Value, _Alloc, std::__detail::_Identity, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Tp, _Hash> > >::value, true, true> >::iterator = std::__detail::_Node_iterator<Movie, true, true>; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::value_type = Movie] <near match>
       insert(value_type&& __x)
       ^~~~~~
/usr/include/c++/6/bits/unordered_set.h:416:7: note:   passing ‘const std::unordered_set<Movie>*’ as ‘this’ argument discards qualifiers
/usr/include/c++/6/bits/unordered_set.h:441:7: note: candidate: std::unordered_set<_Value, _Hash, _Pred, _Alloc>::iterator std::unordered_set<_Value, _Hash, _Pred, _Alloc>::insert(std::unordered_set<_Value, _Hash, _Pred, _Alloc>::const_iterator, const value_type&) [with _Value = Movie; _Hash = std::hash<Movie>; _Pred = std::equal_to<Movie>; _Alloc = std::allocator<Movie>; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::iterator = std::__detail::_Node_iterator<Movie, true, true>; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::const_iterator = std::__detail::_Node_const_iterator<Movie, true, true>; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::value_type = Movie]
       insert(const_iterator __hint, const value_type& __x)
       ^~~~~~
/usr/include/c++/6/bits/unordered_set.h:441:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/6/bits/unordered_set.h:445:7: note: candidate: std::unordered_set<_Value, _Hash, _Pred, _Alloc>::iterator std::unordered_set<_Value, _Hash, _Pred, _Alloc>::insert(std::unordered_set<_Value, _Hash, _Pred, _Alloc>::const_iterator, std::unordered_set<_Value, _Hash, _Pred, _Alloc>::value_type&&) [with _Value = Movie; _Hash = std::hash<Movie>; _Pred = std::equal_to<Movie>; _Alloc = std::allocator<Movie>; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::iterator = std::__detail::_Node_iterator<Movie, true, true>; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::const_iterator = std::__detail::_Node_const_iterator<Movie, true, true>; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::value_type = Movie]
       insert(const_iterator __hint, value_type&& __x)
       ^~~~~~
/usr/include/c++/6/bits/unordered_set.h:445:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/6/bits/unordered_set.h:460:2: note: candidate: template<class _InputIterator> void std::unordered_set<_Value, _Hash, _Pred, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Value = Movie; _Hash = std::hash<Movie>; _Pred = std::equal_to<Movie>; _Alloc = std::allocator<Movie>]
  insert(_InputIterator __first, _InputIterator __last)
  ^~~~~~
/usr/include/c++/6/bits/unordered_set.h:460:2: note:   template argument deduction/substitution failed:
prog.cpp:72:53: note:   candidate expects 2 arguments, 1 provided
         itr->movies.insert(Movie("Stir Crazy", 1981));
                                                     ^
In file included from /usr/include/c++/6/unordered_set:48:0,
                 from prog.cpp:3:
/usr/include/c++/6/bits/unordered_set.h:471:7: note: candidate: void std::unordered_set<_Value, _Hash, _Pred, _Alloc>::insert(std::initializer_list<typename std::_Hashtable<_Value, _Value, _Alloc, std::__detail::_Identity, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Tp, _Hash> > >::value, true, true> >::value_type>) [with _Value = Movie; _Hash = std::hash<Movie>; _Pred = std::equal_to<Movie>; _Alloc = std::allocator<Movie>; typename std::_Hashtable<_Value, _Value, _Alloc, std::__detail::_Identity, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Tp, _Hash> > >::value, true, true> >::value_type = Movie]
       insert(initializer_list<value_type> __l)
       ^~~~~~
/usr/include/c++/6/bits/unordered_set.h:471:7: note:   no known conversion for argument 1 from ‘Movie’ to ‘std::initializer_list<Movie>’
stdout
Standard output is empty