Reputation: 1707
I am trying to implement Graph in C++.
I have defined a class Edge
that take the node name and weight as 2 parameters.
And a class Graph
, When I tried to pass Edge as a template parameter in Graph declaration Graph<int,Edge> g
, I got an error.
Can't I pass a class as template parameter. I am new to C++ coding, so please pardon me for any stupidity. Can anyone suggest the correct way to do it?
template<class T1,class T2>
class Edge{
T1 d_vertex;
T2 d_weight;
public:
Edge(T1,T2);
T1 vertex();
T2 weight();
};
template<class T1,class T2>
Edge<T1,T2>::Edge(T1 v,T2 w):d_vertex(v),d_weight(w){
}
template<class T1,class T2>
T1 Edge<T1,T2>:: vertex(){
return d_vertex;
}
template<class T1,class T2>
T2 Edge<T1,T2>::weight(){
return d_weight;
}
template<class T,class T2>
class Graph{
vector<pair<T, list<T2> > > node;
};
int main()
{
Graph<int,Edge> g;
}
Upvotes: 1
Views: 756
Reputation: 31519
In this instantiation
Graph<int,Edge> g;
Edge
is still a class template. That implies that either your Graph
class should be like that
template<class T, template<class,class> class T2>
class Graph{ /**/ };
ie having a template template parameter or you should specify the type of the Edge
, eg
Graph<int, Edge<int,int>> g;
Upvotes: 2