के साथ मानचित्र मेरे पास std :: weak_ptr का उपयोग std :: map के लिए कुंजी के रूप में करने के बारे में कोई प्रश्न है।std :: std :: weak_ptr key
#include <map>
#include <memory>
int main()
{
std::map< std::weak_ptr<int>, bool > myMap;
std::shared_ptr<int> sharedptr(new int(5));
std::weak_ptr<int> weakptr = sharedptr;
myMap[weakptr] = true;
return 0;
}
उपरोक्त कार्यक्रम का निर्माण नहीं करता है और संकलित करने के लिए कोशिश कर रहा है जैसे कई त्रुटि संदेश देता है: निम्न पंक्ति के कारण
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::tr1::weak_ptr<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(1885) : see declaration of 'std::operator <'
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=std::tr1::weak_ptr<int>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\map(71) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1> with
1> [
1> _Ty=std::tr1::weak_ptr<int>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(451) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
1> with
1> [
1> _Kty=std::tr1::weak_ptr<int>,
1> _Ty=bool,
1> _Pr=std::less<std::tr1::weak_ptr<int>>,
1> _Alloc=std::allocator<std::pair<const std::tr1::weak_ptr<int>,bool>>,
1> _Mfl=false
1> ]
समस्या तब होती है:
myMap[weakptr] = true;
त्रुटि संदेश ऑपरेटर < से संबंधित प्रतीत होते हैं। क्या मुझे कमजोर_ptrs के लिए ऑपरेटर < परिभाषित करने की आवश्यकता है? एक std :: मानचित्र के लिए एक कुंजी प्रकार के रूप में डेटा प्रकार का उपयोग करने के लिए वास्तव में क्या ऑपरेटरों को परिभाषित करने की आवश्यकता है?
अंतिम फॉर्म 'std :: map, यू, std :: owner_less >>' –
DiB