2012-02-08 42 views
0
template<class V, class E> 
class G 
{ 
    public: 
      G(); 
      void InsertVertex(const V&); 
      void InsertEdge(const V&, const V&, const E&); 
    private: 
      typedef set<V,less<V> > vSet; 
      typedef pair<const V,V> ePair; 
      typedef multimap<V,V,less<V> > eSet; 
      typedef map<ePair,E, less<ePair> > edgeValueMap; 
      vSet vertices; 
      eSet edges; 
      edgeValueMap edgeVals; 

}; 

template<class V,class E> 
G<V,E>::G(){} 

template<class V,class E> 
void G<V,E>::InsertVertex(const V& a) 
{ 
    vertices.insert(a); 
} 

template<class V,class E> 
void G<V,E>::InsertEdge(const V& a,const V& b, const E& val) 
{ 
    //create a pair 
    ePair<const V,v> e(a,b); 
    edges.insert(e); 
    edgeVals.insert(e,val); 

} 


int main() 
{ 
    G<char,int> g; 
    g.InsertVertex('a'); 
    g.InsertVertex('b'); 
    g.InsertVertex('c'); 
    g.InsertEdge('a','b',1); 
    return 0; 

} 

而我创建使用 “ePair E(A,B)” 我收到错误的一对: “template2.cpp:39:2:错误: 'G :: ePair' 不是模板” 我我不确定为什么这个编译错误即将到来?我错过了什么吗?如何在C++中初始化这个模板对?

+3

ePair是一个std ::对'与已模板参数typedef''d。 – 2012-02-08 10:54:16

+0

请显示'ePair'的声明。 – 2012-02-08 11:29:29

回答

0

我正在使用make_pair构建实际的地图条目,它在这里工作。但要注意,还调用edgeVals.insert(e,val);报错:所以我修改也:

template<class V,class E> 
void G<V,E>::InsertEdge(const V& a,const V& b, const E& val) 
{ 
    //create a pair 
    ePair e = make_pair(a,b); 
    edges.insert(e); 
    edgeVals[e] = val; 
} 
+0

你真棒。非常感谢。 – namus 2012-02-08 13:05:40