1

I have a custom class, Coord (stands for Coordinates). When I try to create a constructor of Coord with its x and y variables, it works fine. However, when I try to store a the Coord in an unordered_map, it fails.

Coord Class

class Coord{ 
    public:
        int x, y;

    public:
        Coord(int x1, int y1) {
            x = x1;
            y = y1;
        }
        int getX(void) const{
            return x;
        }
        int getY(void) const{
            return y;
        }
        bool operator<(const Coord& other) const
        { return x<other.x || (!(x<other.x) && y<other.y); }
        bool operator==(const Coord &other) const
        {
            return (x == other.x && y == other.y);
        }
};

I have no problem initialising a Coord object with x and y values

Coord start = Coord(0,0);

I created a unordered_map object, and tried to insert Coord objects into it. The code below works.

unordered_map<Coord, Coord> came_from;
Coord start = Coord(0,0);

came_from.insert({start, start});

Then I tried to subscript. This gave me a no matching constructor for initialisation of 'Coord'

unordered_map<Coord, Coord> came_from;
Coord start = Coord(0,0);
came_from[start] = start;

Full Error Message

error: no matching constructor for
      initialization of 'Coord'
      second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:487:11: note: in instantiation of function template
      specialization 'std::__1::pair<const Coord, Coord>::pair<const Coord &, 0>' requested here
        : pair(__pc, __first_args, __second_args,
          ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1805:31: note: in instantiation of function template
      specialization 'std::__1::pair<const Coord, Coord>::pair<const Coord &>' requested here
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1715:18: note: in instantiation of function template
      specialization 'std::__1::allocator<std::__1::__hash_node<std::__1::__hash_value_type<Coord, Coord>, void *> >::construct<std::__1::pair<const Coord,
      Coord>, const std::__1::piecewise_construct_t &, std::__1::tuple<const Coord &>, std::__1::tuple<> >' requested here
            {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1561:14: note: in instantiation of function template
      specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__hash_node<std::__1::__hash_value_type<Coord, Coord>, void *> >
      >::__construct<std::__1::pair<const Coord, Coord>, const std::__1::piecewise_construct_t &, std::__1::tuple<const Coord &>, std::__1::tuple<> >'
      requested here
            {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__hash_table:2305:20: note: in instantiation of function
      template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__hash_node<std::__1::__hash_value_type<Coord, Coord>, void *> >
      >::construct<std::__1::pair<const Coord, Coord>, const std::__1::piecewise_construct_t &, std::__1::tuple<const Coord &>, std::__1::tuple<> >' requested
      here
    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__hash_table:2021:29: note: in instantiation of function
      template specialization 'std::__1::__hash_table<std::__1::__hash_value_type<Coord, Coord>, std::__1::__unordered_map_hasher<Coord,
      std::__1::__hash_value_type<Coord, Coord>, std::__1::hash<Coord>, true>, std::__1::__unordered_map_equal<Coord, std::__1::__hash_value_type<Coord,
      Coord>, std::__1::equal_to<Coord>, true>, std::__1::allocator<std::__1::__hash_value_type<Coord, Coord> > >::__construct_node_hash<const
      std::__1::piecewise_construct_t &, std::__1::tuple<const Coord &>, std::__1::tuple<> >' requested here
        __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:1386:21: note: in instantiation of function
      template specialization 'std::__1::__hash_table<std::__1::__hash_value_type<Coord, Coord>, std::__1::__unordered_map_hasher<Coord,
      std::__1::__hash_value_type<Coord, Coord>, std::__1::hash<Coord>, true>, std::__1::__unordered_map_equal<Coord, std::__1::__hash_value_type<Coord,
      Coord>, std::__1::equal_to<Coord>, true>, std::__1::allocator<std::__1::__hash_value_type<Coord, Coord> > >::__emplace_unique_key_args<Coord, const
      std::__1::piecewise_construct_t &, std::__1::tuple<const Coord &>, std::__1::tuple<> >' requested here
    return __table_.__emplace_unique_key_args(__k,
                    ^
lab1.cpp:341:14: note: in instantiation of member function 'std::__1::unordered_map<Coord, Coord, std::__1::hash<Coord>, std::__1::equal_to<Coord>,
      std::__1::allocator<std::__1::pair<const Coord, Coord> > >::operator[]' requested here
    came_from[start] = start;
             ^
lab1.cpp:34:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class Coord{ 
      ^
lab1.cpp:34:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
lab1.cpp:39:9: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
        Coord(int x1, int y1) {

I have looked at the duplicated link you showed me, but I am not sure how it solves this problem. Can you please help? Cause it hashes perfectly fine

cruise_lab
  • 649
  • 8
  • 21

0 Answers0