下标C ++ unordered_map导致没有匹配的构造函数来初始化'Coord'错误

时间:2019-02-18 02:32:09

标签: c++

我有一个自定义类Coord(代表坐标)。当我尝试使用x和y变量创建Coord的构造函数时,它可以正常工作。但是,当我尝试将坐标存储在unordered_map中时,它将失败。

协调员班级

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);
        }
};

用x和y值初始化Coord对象没有问题

Coord start = Coord(0,0);

我创建了一个unordered_map对象,并尝试将Coord对象插入其中。下面的代码有效。

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

came_from.insert({start, start});

然后我尝试下标。这给了我一个没有匹配的构造函数 初始化“ Coord”

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

完整错误消息

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) {

我查看了您显示给我的重复链接,但不确定如何解决此问题。你能帮忙吗?因为哈希值很好

0 个答案:

没有答案