无法将智能指针插入地图

Unable to insert smart pointer into map

本文关键字:插入 地图 指针 智能      更新时间:2023-10-16

我正在尝试插入我的智能指针,p_pointer<foo>指向一个foo类到map中,但是程序无法编译。因此,我尝试使用普通指针foo*并且程序确实可以编译。到目前为止,我已经能够像普通指针一样使用p_pointer而没有任何问题,所以我很惊讶这不起作用。如果有人能向我解释为什么这行不通......

#include <iostream>
#include<map>
#include<string>
using namespace std;
template <class T>
class p_pointer
{
public:
T* cp;
size_t* refptr;
size_t* counter()
{
return refptr;
}
//default constructor
p_pointer():cp(0),refptr(new size_t(1)) {}
p_pointer(T*t):cp(t),refptr(new size_t(1)) {}
//copy constructor
p_pointer (const p_pointer&s):cp(s.cp),refptr(s.refptr)
{
refptr=s.refptr;
cp=s.cp;
*refptr=*refptr+1;
}
//destructor
~p_pointer()
{
if(--*refptr==0)
{
delete cp;
delete refptr;
}
}
//assignment operator
p_pointer&operator=(const p_pointer&s)
{
++*s.refptr;
//freeing the left hand size if it is the last one
if(--*refptr==0)
{
delete cp;
delete refptr;
}
cp=s.cp;
refptr=s.refptr;
}
operator bool()
{
return cp;
}
T*&operator->()
{
if(cp)
return cp;
else throw std::runtime_error("uninitialized player");
}
T operator*()
{
if(cp)
return *cp;
else throw std::runtime_error("uninitialized player");
}
};
class foo
{};

方法1(有效(

int main()
{
map<foo*,int> x;
foo* y=new foo();
x[y]=1;
}

方法 2 (不起作用(

int main()
{
map<p_pointer<foo>,int> x;
p_pointer<foo> y=new foo();
x[y]=1;
}

错误消息是: 这是我第一次发布错误消息。我只是从构建消息中复制了所有内容并将其粘贴到此处。如果这不是最佳方法,请告诉我

||=== Build: Debug in trial 821 (compiler: GNU GCC Compiler) ===|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h||In instantiation of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = p_pointer<foo>]':|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_map.h|498|required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = p_pointer<foo>; _Tp = int; _Compare = std::less<p_pointer<foo> >; _Alloc = std::allocator<std::pair<const p_pointer<foo>, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = p_pointer<foo>]'|
C:trial 821main.cpp|80|required from here|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|error: no match for 'operator<' (operand types are 'const p_pointer<foo>' and 'const p_pointer<foo>')|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note: candidates are:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note: operator<(int, int) <built-in>|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   no known conversion for argument 2 from 'const p_pointer<foo>' to 'int'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_pair.h|220|note: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_pair.h|220|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::pair<_T1, _T2>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_iterator.h|298|note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_iterator.h|298|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::reverse_iterator<_Iterator>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_iterator.h|348|note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_iterator.h|348|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::reverse_iterator<_Iterator>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_iterator.h|1072|note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_iterator.h|1072|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::move_iterator<_Iterator>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_iterator.h|1078|note: template<class _Iterator> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_iterator.h|1078|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::move_iterator<_Iterator>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsbasic_string.h|2588|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsbasic_string.h|2588|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsbasic_string.h|2600|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsbasic_string.h|2600|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsbasic_string.h|2612|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsbasic_string.h|2612|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   mismatched types 'const _CharT*' and 'p_pointer<foo>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_tree.h|980|note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_tree.h|980|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++array|242|note: template<class _Tp, unsigned int _Nm> bool std::operator<(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++array|242|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::array<_Tp, _Nm>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++tuple|857|note: template<class ... _TElements, class ... _UElements> constexpr bool std::operator<(const std::tuple<_Args1 ...>&, const std::tuple<_Args2 ...>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++tuple|857|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::tuple<_Args1 ...>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_map.h|1017|note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_map.h|1017|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::map<_Key, _Tp, _Compare, _Alloc>'|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_multimap.h|920|note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_multimap.h|920|note:   template argument deduction/substitution failed:|
C:CodeBlocksMinGWlibgccmingw324.9.2includec++bitsstl_function.h|371|note:   'const p_pointer<foo>' is not derived from 'const std::multimap<_Key, _Tp, _Compare, _Alloc>'|
||=== Build failed: 1 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

您的问题可以在错误消息中找到:no match for 'operator<' (operand types are 'const p_pointer' and 'const p_pointer')|.为了将map与自定义类型一起使用(在本例中为p_pointer(,该类型需要声明一个比较运算符<用于对映射中的键进行排序。您需要实现<运算符,这应该可以修复此错误。(不过,可能会出现其他错误来替换它。

同样,我建议只使用现有的std::shared_ptr而不是自己重新实现它,除非您有理由不这样做。