Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用STL容器保存基于模板的共享ptr?_C++_Templates_Stl_Containers_Shared Ptr - Fatal编程技术网

C++ 如何使用STL容器保存基于模板的共享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

我想构造一个容器来保存基于模板的共享的\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 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标准::映射类型;
//                            ^^^^^^^^