c++ - Unable to insert smart pointer into map -
i trying insert smart pointer, p_pointer pointing towards foo class map, program not compile. therefore tried use normal pointer foo* , program compile. far, have been able use p_pointer normal pointer without issue surprised not work. if can explain me why not work...
*additionally, have posted here , people have complained code being messy before. time made use of code blocks function "format use astyle". please let me know if still not enough.
#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 left hand size if last 1 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; }
the error message is: first time posting error message. copied content build messages , paste here. please let me know if not optimal way
||=== build: debug in trial 821 (compiler: gnu gcc compiler) ===| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h||in instantiation of '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|required '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|required here| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|error: no match 'operator<' (operand types 'const p_pointer' , 'const p_pointer')| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: candidates are:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: operator<(int, int) | c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: no known conversion argument 2 'const p_pointer' 'int'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_pair.h|220|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::pair<_t1, _t2>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|298|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::reverse_iterator<_iterator>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|348|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::reverse_iterator<_iterator>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|1072|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::move_iterator<_iterator>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_iterator.h|1078|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::move_iterator<_iterator>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2588|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::basic_string<_chart, _traits, _alloc>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2600|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::basic_string<_chart, _traits, _alloc>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_string.h|2612|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: mismatched types 'const _chart*' , 'p_pointer'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_tree.h|980|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::_rb_tree<_key, _val, _keyofvalue, _compare, _alloc>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\array|242|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::array<_tp, _nm>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\tuple|857|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::tuple<_args1 ...>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_map.h|1017|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::map<_key, _tp, _compare, _alloc>'| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_multimap.h|920|note: template 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|note: template argument deduction/substitution failed:| c:\codeblocks\mingw\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_function.h|371|note: 'const p_pointer' not derived 'const std::multimap<_key, _tp, _compare, _alloc>'| ||=== build failed: 1 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
your problem can found in error message: no match 'operator<' (operand types 'const p_pointer' , 'const p_pointer')|
. in order use map
custom type (p_pointer
in case), type needs declare comparison operator <
used sort keys in map. you'll need implement <
operator, , should fix error. (other errors may show replace though.)
again, recommend using existing std::shared_ptr
rather re-implementing yourself, unless have reason not so.
Comments
Post a Comment