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++ 嵌套模板(即模板<;typename T<;typename templateArgumentFor_T>;)_C++_Templates - Fatal编程技术网

C++ 嵌套模板(即模板<;typename T<;typename templateArgumentFor_T>;)

C++ 嵌套模板(即模板<;typename T<;typename templateArgumentFor_T>;),c++,templates,C++,Templates,我需要为我的类获取2个类型参数:T1,它是一个有模板的类,T2,它是T1的模板的参数 在我的例子中,顶点类型(有两个,一个从另一个继承)和顶点存储的数据类型(在我的例子中是name/id) 我希望能够写出这样的东西: template < typename VertexType < typename VertexIDType > > class Graph { public: Graph(const List<VertexType<

我需要为我的类获取2个类型参数:T1,它是一个有模板的类,T2,它是T1的模板的参数

在我的例子中,顶点类型(有两个,一个从另一个继承)和顶点存储的数据类型(在我的例子中是name/id)

我希望能够写出这样的东西:

template <   typename VertexType  < typename VertexIDType >    >
class Graph
{
public:    
    Graph(const List<VertexType<VertexIDType>>& verticesList); 
    VertexType<VertexIDType>& getVertexByID(const VertexIDType& ID) const;

private:    
    List<VertexType<VertexIDType>> vertices;
};
template>
(这给了我一个错误:C2143语法错误:missing','before'>

(这也给了我错误C2143)

我真的是那种试图自己解决所有问题的人,但这让我感到沮丧。如果/如何在代码中实现,我找不到一个我理解的答案。 我现在已经完成了一门OOP(c++)课程。我有一些模板方面的经验。我编写的模板成功地获得了1个或2个参数,但没有这样的模板

请帮我解决这个问题,最好尽量优雅:)


谢谢。

您可以使用模板参数:

template <template <typename> class VertexType, typename VertexIDType>
class graph;

graph<MyVertexType, MyVertexIDType> //usage
模板
类图;
图表//用法
或者,您可以只获取一个类型,并在部分专门化中提取ID类型:

template <typename Vertex>
class graph;

template <template <typename> class VertexType, typename VertexIDType>
class graph <VertexType<VertexIDType>> {
    //...
}; 

graph<MyVertexType<MyVertexIDType>> //usage
模板
类图;
模板
类图{
//...
}; 
图表//用法

对于您提出的问题,塔坦拉玛的答案是一个很好的答案,但您可能希望稍微改变一下方法。如果要求
VertexType
必须定义typedef
VertexIDType
,则可以编写:

template <class VertexType>
class Graph
{
public:    
    Graph(const List<VertexType>& verticesList); 
    typedef typename VertexType::VertexIDType VertexIDType;
    VertexType& getVertexByID(const VertexIDType& ID) const;

private:    
    List<VertexType> vertices;
};
您需要将其更改为:

template <classname VertexIDType>
struct VType1 {
     double stuff;
     typedef VertexIDType VertexIDType;  // Provide a typedef for VertexIDType.
};
模板
结构VType1{
双重材料;
typedef VertexIDType VertexIDType;//为VertexIDType提供typedef。
};

这类似于标准库所采用的方法,即每个类型(作为容器)都有一个用于
value\u type
等的typedef。能否详细说明“typedef typename”定义?我没有真正理解语法。第一个“VertexIDType”代表什么?在“VertexType”定义中它是什么样子的(我猜您希望它是什么样子,因此是“:”)?第二个“VertexIDType”代表什么?另外,在我的代码中,“VertexType”中没有定义“VertexIDType”。它只是在那里表示为一个模板参数。这不是一门课。如果将来需要,它代表int、long、char、string等或非标准类型(类)。因此,我们的想法是更改
VertexType
,以便在其中定义
VertexIDType
。我的编辑有帮助吗?@DrorFelman:VertexIDType是VertextType的模板参数吗?是的,VertexIDType是类“Vertex”的模板参数。你的意思是用一个模板,比如:
template struct Vertex{typedef VertexIDtype IDType;//Body}
然后
模板类图{typedef typename VertexType::IDType VertexIDType;//Body–与您编写的相同}并像这样实例化它,例如:
Graph g当我考虑这个问题时,这也是一个很好的选择——从类本身提取类型信息。专门化会像预期的那样工作吗?据我所知,编译器总是选择参数最少的模板,这里是
template
template <classname VertexIDType>
struct VType1 {
     double stuff;
     typedef VertexIDType VertexIDType;  // Provide a typedef for VertexIDType.
};