我正在尝试插入我的智能指针,p_pointer指向foo类进入map,但程序无法编译。因此,我试图使用普通指针foo *,程序确实编译。到目前为止,我已经能够像普通指针一样使用p_pointer而没有任何问题,所以我很惊讶这不起作用。如果有人能向我解释为什么这不起作用......
*此外,我已经发布在这里,人们抱怨我的代码之前很乱。这次我使用了代码块功能"格式化使用Astyle"。如果这还不够好,请告诉我。
#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
{};
//Method 1(work)
int main()
{
map<foo*,int> x;
foo* y=new foo();
x[y]=1;
}
//Method 2 (does not work)
int main()
{
map<p_pointer<foo>,int> x;
p_pointer<foo> y=new foo();
x[y]=1;
}
错误消息是: 这是我第一次发布错误消息。我刚从构建消息中复制了所有内容并将其粘贴到此处。如果这不是最佳方式,请告诉我
|| === Build:在试用版821中调试(编译器:GNU GCC编译器)=== | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h ||实例化&#39; bool std :: less&lt; _Tp&gt; :: operator()(const _Tp&amp;,const _Tp&amp;)const [with _Tp = p_pointer]&#39;:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_map.h | 498 |必需来自&#39; std :: map&lt; _Key,_Tp,_Compare,_Alloc&gt; :: mapped_type&安培; std :: map&lt; _Key,_Tp,_Compare,_ Alloc&gt; :: operator [](const key_type&amp;)[with _Key = p_pointer; _Tp = int; _Compare = std :: less&gt ;; _Alloc = std :: allocator,int&gt;取代; std :: map&lt; _Key,_Tp,_Compare,_Alloc&gt; :: mapped_type = int; std :: map&lt; _Key,_Tp,_Compare,_Alloc&gt; :: key_type = p_pointer]&#39; | C:\ trial 821 \ main.cpp | 80 |从这里要求| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |错误:不匹配&#39;运算符&lt;&#39; (操作数类型是&#39; const p_pointer&#39;和&#39; const p_pointer&#39;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:候选人是:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:operator&lt;(int,int)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:没有来自&#39; const p_pointer&#39;的参数2的已知转换到&#39; int&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_pair.h | 220 |注意:模板constexpr bool std :: operator&lt;(const std :: pair&lt; _T1,_T2&gt; &amp;,const std :: pair&lt; _T1,_T2&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_pair.h | 220 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源于&#39; const std :: pair&lt; _T1,_T2&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_iterator.h | 298 |注意:模板bool std :: operator&lt;(const std :: reverse_iterator&lt; _Iterator&gt;&amp;, const std :: reverse_iterator&lt; _Iterator&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_iterator.h | 298 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源自&#39; const std :: reverse_iterator&lt; _Iterator&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_iterator.h | 348 |注意:模板bool std :: operator&lt;(const std :: reverse_iterator&lt; _Iterator&gt;&amp;, const std :: reverse_iterator&lt; _IteratorR&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_iterator.h | 348 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源自&#39; const std :: reverse_iterator&lt; _Iterator&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_iterator.h | 1072 |注意:模板bool std :: operator&lt;(const std :: move_iterator&lt; _Iterator&gt;&amp ;, const std :: move_iterator&lt; _IteratorR&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_iterator.h | 1072 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源于&#39; const std :: move_iterator&lt; _Iterator&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_iterator.h | 1078 |注意:模板bool std :: operator&lt;(const std :: move_iterator&lt; _Iterator&gt;&amp;, const std :: move_iterator&lt; _Iterator&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_iterator.h | 1078 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源于&#39; const std :: move_iterator&lt; _Iterator&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ basic_string.h | 2588 |注意:模板bool std :: operator&lt;(const std :: basic_string&lt; _CharT,_Traits,_Alloc&gt ;&amp;,const std :: basic_string&lt; _CharT,_Traits,_Alloc&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ basic_string.h | 2588 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源自&#39; const std :: basic_string&lt; _CharT,_Traits,_Alloc&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ basic_string.h | 2600 |注意:模板bool std :: operator&lt;(const std :: basic_string&lt; _CharT,_Traits,_Alloc&gt ;&amp;,const _CharT *)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ basic_string.h | 2600 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源自&#39; const std :: basic_string&lt; _CharT,_Traits,_Alloc&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ basic_string.h | 2612 |注意:模板bool std :: operator&lt;(const _CharT *,const std :: basic_string&lt; _CharT ,_Traits,_Alloc&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ basic_string.h | 2612 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:不匹配的类型&#39; const _CharT *&#39;和&#39; p_pointer&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_tree.h | 980 |注意:模板bool std :: operator&lt;(const std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue ,_Compare,_ Alloc&gt;&amp;,const std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,_Alloc&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_tree.h | 980 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源于&#39; const std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,_ Alloc&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ array | 242 |注意:模板bool std :: operator&lt;(const std :: array&lt; _Tp,_Nm&gt;&amp;,const std :: array&lt; _Tp,_Nm&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ array | 242 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源自&#39; const std :: array&lt; _Tp,_Nm&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ tuple | 857 |注意:模板constexpr bool std :: operator&lt;(const std :: tuple&lt; _Args1 ...&amp;&amp; ,const std :: tuple&lt; _Args2 ...&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ tuple | 857 |注意:模板参数扣除/替换失败: C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源于&#39; const std :: tuple&lt; _Args1 ...&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_map.h | 1017 |注意:模板bool std :: operator&lt;(const std :: map&lt; _Key,_Tp,_Compare ,_Alloc&gt;&amp;,const std :: map&lt; _Key,_Tp,_Compare,_Alloc&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_map.h | 1017 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源于&#39; const std :: map&lt; _Key,_Tp,_Compare,_Alloc&gt;&#39; | C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_multimap.h | 920 |注意:模板bool std :: operator&lt;(const std :: multimap&lt; _Key,_Tp,_Compare ,_Alloc&gt;&amp;,const std :: multimap&lt; _Key,_Tp,_Compare,_Alloc&gt;&amp;)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_multimap.h | 920 |注意:模板参数扣除/替换失败:| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c ++ \ bits \ stl_function.h | 371 |注意:&#39; const p_pointer&#39;不是源于&#39; const std :: multimap&lt; _Key,_Tp,_Compare,_Alloc&gt;&#39; | || ===构建失败:1个错误,3个警告(0分钟,0秒(秒))=== |
答案 0 :(得分:1)
您的问题可以在错误消息中找到:no match for 'operator<' (operand types are 'const p_pointer' and 'const p_pointer')|
。为了将map
与自定义类型(在这种情况下为p_pointer
)一起使用,该类型需要声明一个比较运算符<
,用于对映射中的键进行排序。您需要实现<
运算符,这应该可以解决此错误。 (但是可能会出现其他错误以替换它。)
同样,我建议您只使用现有的std::shared_ptr
,而不是自己重新实施,除非您有某些理由不这样做。