我在 C 中定義了兩個類,如下所示。
#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 中收到一條錯誤訊息,提示“使用類模板節點需要模板引數”。我完全失去了任何幫助,非常感謝。
uj5u.com熱心網友回復:
方案一
您可以通過指定一個型別來解決這個問題,std::vector<node*> Nodes; 如下所示:
std::vector<node<int>*> Nodes; //note i have added int you can add any type
解決方案2
另一種解決方案是使類DAG成為類模板,如下所示:
template<typename T>
class DAG {//Class for the graph
std::vector<node<T>*> Nodes;
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/399079.html
上一篇:存盤格式化的字串,稍后傳入值?
下一篇:模板引數檢查的通用方法
