C++ 如何使用STL容器保存基于模板的共享ptr?
我想构造一个容器来保存基于模板的共享的\u ptr。例如,我有:C++ 如何使用STL容器保存基于模板的共享ptr?,c++,templates,stl,containers,shared-ptr,C++,Templates,Stl,Containers,Shared Ptr,我想构造一个容器来保存基于模板的共享的\u ptr。例如,我有: template <class T> class Data { .... }; template <class T> struct DataPtr { typedef boost::shared_ptr<Data<T> > type; }; template <class T> struct mapData { typedef typename
template <class T>
class Data
{
....
};
template <class T>
struct DataPtr {
typedef boost::shared_ptr<Data<T> > type;
};
template <class T>
struct mapData {
typedef typename std::map<std::string, DataPtr<T>::type > type;
};
mapData<int>::type data;
void func(const std::string& str, DataPtr<int>::type& sth)
{
if (sth)
{
data[str] = sth;
}
}
模板
类数据
{
....
};
模板
结构数据ptr{
typedef boost::共享_ptr类型;
};
模板
结构映射数据{
typedef typename std::映射类型;
};
mapData::类型数据;
void func(const std::string&str,DataPtr::type&sth)
{
如果(某事物)
{
数据[结构]=某物;
}
}
现在我有几个问题。编译器不允许我在定义mapData时使用DataPtr::type,错误消息为“需要一个类型,得到'DataPtr::type'。如果我放弃类型并使用
template <class T>
struct mapData {
typedef typename std::map<std::string, DataPtr<T> > type;
};
模板
结构映射数据{
typedef typename std::映射类型;
};
然后“data[str]=sth”不通过(“operator='不匹配”)
正确的方法应该是什么
非常感谢。您把
typename
关键字的位置放错了:
typedef std::map<std::string, typename DataPtr<T>::type > type;
// ^^^^^^^^
typedef标准::映射类型;
// ^^^^^^^^