提问人:Ninrich 提问时间:6/7/2019 更新时间:6/7/2019 访问量:235
由于派生自 std::map [closed] 的类中的 std::map 迭代器导致内存错误
Memory error due to std::map iterator in a class derived from std::map [closed]
问:
我正在从 std::map 派生一个类,因为我希望为此数据结构创建自己的方法。我在使用“mySelect”时遇到了问题,如果元素不存在,它应该返回nullptr,否则unique_ptr。
我尝试在迭代器声明之前指定 typename 关键字,但无济于事。
template <class KeyType, class ValueType>
class Container : public std::map<KeyType, ValueType> {
public:
std::unique_ptr<ValueType> mySelect(KeyType key) {
typename map<KeyType, ValueType>::iterator value;
if ((value = this->find(key)) == this->end())
return nullptr;
return std::make_unique<ValueType>(value);
}
}
我收到此错误:
Error C2664 'std::vector<std::shared_ptr<Transaction>,std::allocator<_Ty>>::vector(const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 1 from 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>' to 'const _Alloc &'
答:
0赞
Slava
6/7/2019
#1
首先是这段代码:
return std::make_unique<ValueType>(value);
在逻辑上等于:
std::unique_ptr<ValueType> tmp = new Value(value);
return tmp;
(虽然它不一样,所以你不应该用另一个代替一个,只是为了让你理解)。因此,您正在尝试创建一个新的类实例并从迭代器初始化它。除非提供这样的构造函数,否则这将不起作用。如果您想制作副本并在转让所有权时将其归还,请将您的代码更改为:Value
Value
return std::make_unique<ValueType>(value->second);
但我不确定这是你想做的。如果要返回指向现有对象的指针,则不能在此处使用,因为它提供了唯一的所有权(例如名称),您需要按值存储在映射而不是对象中并返回它的副本,或者只是返回原始指针。std::unique_ptr
std::shared_ptr
如何使 mySelect() 的调用方成为返回对象的所有者?
正如我所说,您存储对象并与此方法的调用者共享所有权,或者您最初将对象存储为,但随后您必须将其移出,因为您将无法再拥有该对象。std::shared_ptr
std::unique_ptr
std::map
评论
std::make_unique<ValueType>(value)
Container
std::make_unique
std::unique_ptr