class 模板节点的使用需要模板参数 C++
Use of class template node requires template arguments C++
我在 C++ 中定义了两个 classes,如下所示。
#include <vector>
#include <tuple>
#include <map>
template <class T> class node {
public:
int NodeID; //ID used to identify node when inserting/deleting/finding.
T data; //generic data encapsulated in each node.
std::vector<node*> children; //child nodes, list of ptrs
std::vector<node*> parents; //parent nodes list of ptrs
};
class DAG {//Class for the graph
std::vector<node*> Nodes;
}
然而,我在 DAG 中收到一条错误消息,提示“class 模板节点的使用需要模板参数”。我完全迷失了任何帮助,非常感谢。
解决方案 1
您可以通过在 std::vector<node*> Nodes;
中指定类型来 解决 问题,如下所示:
std::vector<node<int>*> Nodes; //note i have added int you can add any type
解决方案 2
另一种解决方案是制作 class DAG
一个 class 模板 ,如下所示:
template<typename T>
class DAG {//Class for the graph
std::vector<node<T>*> Nodes;
};
我在 C++ 中定义了两个 classes,如下所示。
#include <vector>
#include <tuple>
#include <map>
template <class T> class node {
public:
int NodeID; //ID used to identify node when inserting/deleting/finding.
T data; //generic data encapsulated in each node.
std::vector<node*> children; //child nodes, list of ptrs
std::vector<node*> parents; //parent nodes list of ptrs
};
class DAG {//Class for the graph
std::vector<node*> Nodes;
}
然而,我在 DAG 中收到一条错误消息,提示“class 模板节点的使用需要模板参数”。我完全迷失了任何帮助,非常感谢。
解决方案 1
您可以通过在 std::vector<node*> Nodes;
中指定类型来 解决 问题,如下所示:
std::vector<node<int>*> Nodes; //note i have added int you can add any type
解决方案 2
另一种解决方案是制作 class DAG
一个 class 模板 ,如下所示:
template<typename T>
class DAG {//Class for the graph
std::vector<node<T>*> Nodes;
};