我有一个包含在 boost::intrusive_ptr 中的 Locker 类型的小模板类,我想将它存储在 std::map 中:template <typename T>bool LockerManager<T>:: AddData(const std::string& id, T* pData){ boost::intrusive_ptr<Locker<T> > lPtr(Locker<T>(pData)); // Line 359 - compiles mMap.insert(make_pair(id, lPtr)); // Line 361 - gives error}Locker 只是一个容器类;它的构造函数看起来像:template <typename T>Locker<T>:: Locker(T* pData) : IntrusivePtrCountable(), mpData(pData), mThreadId(0), mDataRefCount(0){}在我对这门课的测试中,我试图做到以下几点:class Clayton{public: static int count; Clayton() { mNumber = count++;} void GetNumber() { cerr<<"My number is: "<<mNumber<<endl; }private: int mNumber;};int Clayton::count = 0;class ClaytonManager{public: bool AddData(const std::string& id, Clayton* pData) { return mManager.AddData(id, pData); }private: LockerManager<Clayton> mManager;};我收到以下编译错误:Compiling LockerManagerTest.cpp : /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h: In constructor `std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _U2 = boost::intrusive_ptr<Locker<Clayton> > (*)(Locker<Clayton>), _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = boost::intrusive_ptr<Locker<Clayton> >]':../Utilities/include/LockerManager.h:361: instantiated from `bool LockerManager<T>::AddData(const std::string&, T*) [with T = Clayton]'src/LockerManagerTest.cpp:35: instantiated from here/usr/local/lib/gcc/i686-pc-linux-gnu/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:90: error: no matching function for call to `boost::intrusive_ptr<Locker<Clayton> >::intrusive_ptr(boost::intrusive_ptr<Locker<Clayton> > (* const&)(Locker<Clayton>))'/usr/local/boost-1.36.0/include/boost-1_36/boost/intrusive_ptr.hpp:94: note: candidates are: boost::intrusive_ptr<T>::intrusive_ptr(const boost::intrusive_ptr<T>&) [with T = Locker<Clayton>]/usr/local/boost-1.36.0/include/boost-1_36/boost/intrusive_ptr.hpp:70: note: boost::intrusive_ptr<T>::intrusive_ptr(T*, bool) [with T = Locker<Clayton>]/usr/local/boost-1.36.0/include/boost-1_36/boost/intrusive_ptr.hpp:66: note: boost::intrusive_ptr<T>::intrusive_ptr() [with T = Locker<Clayton>]Command exited with non-zero status 10:05.40请帮忙 最佳答案 实际上, intrusive_ptr 已经定义了 我们遗漏了两件主要的事情。首先,我们需要使用 value_type 而不是 make_pair ,以避免 insert 语句中的隐式类型转换。其次,我们忽略了一个事实,即 intrusive_ptr 构造函数采用 指针 指向它被模板化的类型。因此,最终的工作方法如下所示:// ---------------------------------------------------------------------------// LockerManager::AddData// ---------------------------------------------------------------------------template <typename T>bool LockerManager<T>:: AddData(const std::string& id, T* pData){ Lock<MutualExclusion> lLock(mMutex); if ((pData == NULL) || (mMap.find(id) != mMap.end())) return false; mMap.insert(typename std::map<std::string, boost::intrusive_ptr<Locker<T> > >::value_type(id, new Locker<T>(pData))); return true;} // LockerManager::AddData关于c++ - 帮助在 std::map 中存储模板类的 intrusive_ptr,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/741000/ 10-12 19:32