I am trying to write an adjacency list to save the graph.
And here is my code, the class Graph
has a member called Node
. The Node
can save any type of message, and many children with the cost to them.
After Node
thers is a nodes
member to save all of the node in graph.
template<typename T>
class Graph {
public:
struct Node {
using Ptr = std::shared_ptr<Node>;
T val;
std::vector<std::pair<Ptr, int>> children;
Node() {}
Node(T data) : val(data) , children() {}
};
typename std::vector<Graph<T>::Node::Ptr> nodes;
Graph(){}
Graph(size_t n) : nodes(n) {
for(auto node : nodes)
node = std::make_shared<Node>();
}
};
Problem
typename std::vector<Graph<T>::Node::Ptr> nodes;
My compiler doesnot think Ptr
is a valid type, here is the error
type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
I can put the declaration of Node
out of the Graph
and make it a template to solve this problem, but why cannot this?
System Info
- Ubuntu 20.04 (WSL2)
- g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Already read