无法将智能指针插入地图
Unable to insert smart pointer into map
我正在尝试插入我的智能指针,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
而不是自己重新实现它,除非您有理由不这样做。
相关文章:
- 将一对插入地图并增加计数?
- 当我将其插入无序地图时,矢量的容量为 0
- 为什么不插入地图?
- 为什么emplace_hint比插入地图慢?
- 无法将智能指针插入地图
- 如何将元组插入地图?
- 在C++中访问和插入地图的地图(嵌套地图)
- 通过EMPLEPE()将对象指针插入地图中
- 将mpfr_t对象插入地图中的值
- 有效地将值插入地图.更好地增加或减少键
- C :将向量插入地图
- C - 插入地图或向量时的分割故障
- 当我尝试将一对插入地图时,Qt程序seg出错
- C++ 将字符串插入地图时出现编译错误
- 将unique_ptr插入地图,指针被销毁
- 为什么我在插入地图时遇到 seg 错误
- 插入地图时出现分割错误
- 当我插入地图时,会出现Valgrind错误
- 将项目插入地图,其中还有 2 张地图
- 如何将数据插入地图<字符串,设置<string>> C++?