我试图插入我的智能指针,p_pointer指向foo类进入map,但程序无法编译 . 因此,我试图使用普通指针foo *,程序确实编译 . 到目前为止,我已经能够像普通指针一样使用p_pointer而没有任何问题,所以我很惊讶这不起作用 . 如果有人能向我解释为什么这不起作用......
*此外,我已经发布在这里,人们抱怨我的代码之前是凌乱的 . 这次我使用了代码块功能“Format use 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 ||实例化'bool std :: less <_Tp> :: operator()(const _Tp&,const _Tp&)const [with _Tp = p_pointer]':| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_map.h | 498 |从'std :: map <_Key,_Tp,_Compare,_Alloc> :: mapped_type&std中需要: :map <_Key,_Tp,_Compare,_Alloc> :: operator [](const key_type&)[with _Key = p_pointer; _Tp = int; _Compare = std :: less>; _Alloc = std :: allocator,int >>; std :: map <_Key,_Tp,_Compare,_Alloc> :: mapped_type = int; std :: map <_Key,_Tp,_Compare,_Alloc> :: key_type = p_pointer]'| C:\ trial 821 \ main.cpp | 80 |从这里要求| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_function.h | 371 |错误:'operator <'不匹配(操作数类型为'const p_pointer'和'const p_pointer “)| 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 <(int,int)| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_function.h | 371 |注意:参数2从'const p_pointer'到'int'没有已知的转换C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_pair.h | 220 |注意:模板constexpr bool std :: operator <(const std :: pair <_T1,_T2> &,const std :: pair <_T1,_T2>&)| 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 |注意:'const p_pointer'不是从'const std :: pair <_T1,_T2>派生的“| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_iterator.h | 298 |注意:模板bool std :: operator <(const std :: reverse_iterator <_Iterator>&,const的std :: reverse_iterator的<_Iterator>&)| 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 |注意:'const p_pointer'不是从'const std :: reverse_iterator <_Iterator>'派生的C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_iterator.h | 348 |注意:模板bool std :: operator <(const std :: reverse_iterator <_Iterator>&,const的std :: reverse_iterator的<_IteratorR>&)| 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 |注意:'const p_pointer'不是从'const std :: reverse_iterator <_Iterator>'派生的C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_iterator.h | 1072 |注意:模板bool std :: operator <(const std :: move_iterator <_Iterator>&,const的std :: move_iterator <_IteratorR>&)| 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 |注意:'const p_pointer'不是从'const std :: move_iterator <_Iterator>'派生的C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_iterator.h | 1078 |注意:模板bool std :: operator <(const std :: move_iterator <_Iterator>&,const的std :: move_iterator <_Iterator>&)| 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 |注意:'const p_pointer'不是从'const派生的的std :: move_iterator <_Iterator>'| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ basic_string.h | 2588 |注意:模板bool std :: operator <(const std :: basic_string <_CharT,_Traits,_Alloc >&,const std :: basic_string <_CharT,_Traits,_Alloc>&)| 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 |注意:'const p_pointer'不是从'const std :: basic_string <_CharT,_Traits派生的, _Alloc>“| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ basic_string.h | 2600 |注意:模板bool std :: operator <(const std :: basic_string <_CharT,_Traits,_Alloc >&,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 |注意:'const p_pointer'不是从'const std :: basic_string <_CharT,_Traits派生的, _Alloc>“| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ basic_string.h | 2612 |注意:模板bool std :: operator <(const _CharT *,const std :: basic_string <_CharT ,_Traits,_Alloc>&)| 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 |注意:不匹配的类型'const _CharT *'和'p_pointer'| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_tree.h | 980 |注意:模板bool std :: operator <(const std :: _ Rb_tree <_Key,_Val,_KeyOfValue ,Compare, Alloc>&,const std :: _ Rb_tree <_Key,_Val,_KeyOfValue,_Compare,_Alloc>&)| 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 |注意:'const p_pointer'不是从'const std :: _ Rb_tree <_Key,_Val派生的, _KeyOfValue,Compare, Alloc>'| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ array | 242 |注意:模板bool std :: operator <(const std :: array <_Tp,_Nm>&,const std: :array <_Tp,_Nm>&)| 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 |注意:'const p_pointer'不是从'const std :: array <_Tp,_Nm>派生的“| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ tuple | 857 |注意:模板constexpr bool std :: operator <(const std :: tuple <_Args1 ...>&,const std :: tuple <_Args2 ...>&)| 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 |注意:'const p_pointer'不是来自'const std :: tuple <_Args1 ... >“| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_map.h | 1017 |注意:模板bool std :: operator <(const std :: map <_Key,_Tp,_Compare ,_Alloc>&,const std :: map <_Key,_Tp,_Compare,_Alloc>&)| 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 |注意:'const p_pointer'不是从'const std :: map <_Key,_Tp派生的, Compare, Alloc>'| C:\ CodeBlocks \ MinGW \ lib \ gcc \ mingw32 \ 4.9.2 \ include \ c \ bits \ stl_multimap.h | 920 |注意:模板bool std :: operator <(const std :: multimap <_Key,_Tp,_Compare ,_Alloc>&,const std :: multimap <_Key,_Tp,_Compare,_Alloc>&)| 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 |注意:'const p_pointer'不是从'const std :: multimap <_Key,_Tp派生的, Compare, Alloc>'| || ===构建失败:1个错误,3个警告(0分钟,0秒(秒))=== |
1 回答
您可以在错误消息中找到您的问题:
no match for 'operator<' (operand types are 'const p_pointer' and 'const p_pointer')|
. 为了将map
与自定义类型(在本例中为p_pointer
)一起使用,该类型需要声明一个比较运算符<
,用于对映射中的键进行排序 . 您需要实现<
运算符,这应该可以解决此错误 . (但是可能会出现其他错误以替换它 . )再次,我建议只使用现有的
std::shared_ptr
而不是除非你有某些理由不这样做,否则你自己重新实施它 .