gpt4 book ai didi

c++ - 错误 C2663 : 'std::_Hash<_Traits>::insert' : 3 overloads have no legal conversion for 'this' pointer

转载 作者:搜寻专家 更新时间:2023-10-31 01:15:48 25 4
gpt4 key购买 nike

考虑以下代码:

#include <unordered_map>

struct A {};

struct T
{
std::unordered_map<std::string, A> _map;
};

struct L
{
std::shared_ptr<const T> _c;
};


class f {
void oid (std::shared_ptr<L> l, std::string st, A a) {
l->_c->_map.insert(std::make_pair(st,a));
}
};

在编译过程中,它抛出以下错误:

error C2663: 'std::_Hash<_Traits>::insert' : 3 overloads have no legal conversion for 'this' pointer with [ _Traits=std::tr1::_Umap_traits,std::equal_to>,std::allocator>,false> ]

我试图删除 const来自 std::shared_ptr<const T> _c; (不是我认为这很重要),但它构造了一些其他错误......

感谢您的帮助!

最佳答案

您正在尝试插入 unordered_map您已声明为 const , 这是不允许的。为什么是_c里面L一个shared_ptr<const T> ???这有效地使 _map在由共享指针管理的对象上是一个常量对象,您将无法修改它。

关于c++ - 错误 C2663 : 'std::_Hash<_Traits>::insert' : 3 overloads have no legal conversion for 'this' pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9705803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com