【问题标题】:Multiple overloads of 'operator[]' instantiate to the same signature'operator[]' 的多个重载实例化为相同的签名
【发布时间】:2019-03-18 21:46:09
【问题描述】:

我有一个 Objects 列表,其中存储了一些 Data。一旦一个对象被添加到列表中,它将永远不会被删除。给定一些数据,我还想快速查找它来自哪个对象。

我想创建从数据到对象的映射,而不是遍历整个列表并检查每个对象。

#include <vector>
#include <map>

class Data {/*Stuff here*/};

struct Object {
    Data d;
    Object(Data d) : d(d) {}
};

class ObjectStorage {
    std::vector<Object> objects;
    std::map<Data&, Object&> reverse_lookup;

public:
    void add_object(Data d) {
        objects.emplace_back(d);

        auto &obj = objects.back();
        reverse_lookup.emplace(obj.d, obj);
    }
};

int main() {
    ObjectStorage S;
    S.add_object(Data());
}

为了节省空间,我在地图中使用了参考。对象和数据已经存储在列表中,我知道它们永远不会被删除。

但是,我在尝试编译此程序时遇到以下错误。

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:1024:18: error: multiple overloads of 'operator[]' instantiate to the same signature 'std::__1::map<Data &, Object &, std::__1::less<Data &>, std::__1::allocator<std::__1::pair<Data &, Object &> > >::mapped_type &(std::__1::map<Data &, Object &,
      std::__1::less<Data &>, std::__1::allocator<std::__1::pair<Data &, Object &> > >::key_type &)'
    mapped_type& operator[](key_type&& __k);
                 ^
test.cpp:13:27: note: in instantiation of template class 'std::__1::map<Data &, Object &, std::__1::less<Data &>, std::__1::allocator<std::__1::pair<Data &, Object &> > >' requested here
        std::map<Data&, Object&> reverse_lookup;
                                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:1022:18: note: previous declaration is here
    mapped_type& operator[](const key_type& __k);
                 ^
1 error generated.

错误的主要部分似乎是:

错误:'operator[]' 的多个重载实例化为相同的签名

【问题讨论】:

  • 看起来原始问题已得到解答,但您可以通过以下任一方法改进您的解决方案: A. 使用 std::unordered_map 而不是 std::map,查找是 O(1) 而不是O(lg(N)) 所以理论上会快得多。(你必须定义一个哈希函数)。或 B. 由于您要添加“bool operator
  • 稍微偏离主题,但谷歌只能找到错误“地址实例化到相同签名的多个重载”的此链接,对于(我的情况)向量:Does C++ allow vector<const T>?(编号)@987654322 @(编号)

标签: c++ c++11 reference


【解决方案1】:

错误源于使用std::map 中的引用。有关原因的更多信息,请参阅问题"Why Can't I store references in a std::map in C++?"

如果不是太贵,把reverse_lookup的映射改成通过

std::map<Data, Object> reverse_lookup;

并为bool operator&lt;(const Data&amp; lhs, const Data&amp; rhs) 添加一个定义以允许将Data 用作密钥将解决问题。

如果复制DataObject 成本太高,可以使用指针映射代替引用。但是,请注意指向数据的存储方式。在这种情况下,如果objects 向量增长超过其内部容量并被调整大小,所有指针都将变为无效。使用像std::list 这样不会在内存中移动数据的东西应该可以防止这个问题。感谢 Christophe 在 cmets 中指出这一点。

【讨论】:

  • 另外,当需要增加vector的容量时,可以将vector中的对象移动到内存中。所以引用可能会悬空。
  • @Christophe 好点!我相信这也意味着如果在映射中使用指针,如果 objects 向量达到其内部容量并增加其内部大小,它们都将无效。
  • 是的,完全正确!唯一的方法是将 id (但我认为这是数据的想法)映射到向量中的索引,并仅在搜索数据时将索引转换为指针或引用(被理解为可能添加对象后立即失效。
猜你喜欢
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 2015-03-10
  • 1970-01-01
  • 2018-09-16
  • 2010-12-14
  • 2021-03-16
相关资源
最近更新 更多