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++ 指向模板类作为该类参数的指针_C++_Templates - Fatal编程技术网

C++ 指向模板类作为该类参数的指针

C++ 指向模板类作为该类参数的指针,c++,templates,C++,Templates,基本上,在尝试通过邻接列表实现图形时,我在定义图形节点时遇到了困难: template < typename Vertex, typename Edge, typename EdgeLink = std::pair< Edge, GraphNode* >, typename Alloc = std::allocator< EdgeLink > > class GraphNode { public: Vertex ver

基本上,在尝试通过邻接列表实现图形时,我在定义图形节点时遇到了困难:

template <
    typename Vertex, 
    typename Edge,
    typename EdgeLink = std::pair< Edge, GraphNode* >,
    typename Alloc = std::allocator< EdgeLink >
>
class GraphNode
{
public:
    Vertex vertex;
    std::list< EdgeLink, Alloc > neighbours;
};
模板<
typename顶点,
字体名称边缘,
typename EdgeLink=std::pair,
typename Alloc=std::分配器
>
类图节点
{
公众:
顶点;
标准::列表邻居;
};
我意识到我不能给GraphNode模板指针提供参数,因为它们还没有定义。我对C++模板大师的问题是:在这种情况下使用什么技术?< /P>
谢谢。

如果您的EdgeLink将位于GraphNode的“内部”,最好不要将其声明为模板参数。相反,请使用以下方法:

template <
    typename Vertex, 
    typename Edge
>
class GraphNode
{
public:
    typedef GraphNode<Vertex, Edge> NodeType;
    typedef std::pair< Edge, NodeType* > LinkType;
    typedef std::allocator< Linktype > AllocType;
    Vertex vertex;
    std::list< LinkType, AllocType > neighbours;
};
模板<
typename顶点,
字体名称边缘
>
类图节点
{
公众:
typedef图形节点类型;
typedef std::pairLinkType;
typedef std::分配器AllocType;
顶点;
std::listneights;
};

精确计算分配器并不需要精确计算分配器的用途。例如,在
std::list
中,传递的分配器是
std::allocator
,而
list
将分配
\u ListNode
(实现定义)。这是因为分配器需要提供一种机制

template <
    typename Vertex, 
    typename Edge,
    typename Allocator = std::allocator<void*>
>
class GraphNode
{
public:
    typedef GraphNode<Vertex, Edge, Allocator> NodeType;
    typedef std::pair< Edge, NodeType* > LinkType;
    typedef typename Allocator::template rebind<LinkType>::other AllocatorType;

    Vertex vertex;
    std::list< LinkType, AllocatorType > neighbours;
};
可以这样调用:

GraphNode<std::string, int>
GraphNode<std::string, int, std::list>
GraphNode<std::string, int, std::vector>
图形节点
石墨节点
石墨节点

.

您不能使用转发声明吗<代码>类图形节点好的,我不确定问题是什么。。。你想让GraphNode有一个指向另一个具有未知模板参数的GraphNode的指针吗?我不能只写GraphNode*,它需要是GraphNode*,但没有定义EdgeLink和Alloc。用户真的需要指定
EdgeLink
(和
分配器
)?EdgeLink真的没有必要,但是我想让分配器成为模板参数在任何范围内都禁止使用以
\uz]
开头的标识符(保留给实现)。修改了我的答案。感谢您提供的信息:)问题是我希望分配器是一个模板参数。您是否可以将答案修改为分配器作为模板参数?Edge类型包含哪些信息?它已经有关于两个连接节点的信息了吗?Vertex可以是一个城镇名称,Edge可以是一个保持从一个城镇到另一个城镇距离的数字,例如,感谢您提供的信息,这是令人惊奇的,您可以用模板编程来做的事情!另一个快速问题:如何使用模板参数来指定容器,例如使用std::vector而不是std::list?@Borzh:updated:)正如我在回答中所说的,这比应该的要复杂,因为(正确的)分配器只存在于类中。
GraphNode<std::string, int>
GraphNode<std::string, int, std::list>
GraphNode<std::string, int, std::vector>