I am working on Graph Template Class. Here is what I written till now.
#ifndef __GRAPH_H__
#define __GRAPH_H__
#include <map>
#include <list>
template <typename _Ty>
class Graph {
private:
template <typename _Ty>
class Vertex {
public:
Vertex(_Ty in) : m_Label(in) {
}
~Vertex() {
}
private:
_Ty m_Label;
protected:
};
public:
typedef Vertex<_Ty> VertexType;
typedef std::list<VertexType> AdjListType;
typedef std::map<VertexType,AdjListType> GraphType;
public:
Graph(bool bType = false) : m_Type(bType) {
}
~Graph() {
}
void AddEdge(VertexType vLevt, VertexType vRight) {
}
private:
// true if bidirectional
// false if unidirectional.
bool m_Type;
GraphType m_Graph;
protected:
};
#endif
Here is how I am using this class.
#include "Graph.h"
#include <string>
int main(int argc, char **argv) {
Graph<int> myGraph;
myGraph.AddEdge(1,2);
Graph<char *> myGraph2;
myGraph2.AddEdge("A","B");
Graph<std::string> myGraph3;
myGraph3.AddEdge("A","B");
}
myGraph3 is giving me compilation error.
error C2664: 'Graph<_Ty>::AddEdge' : cannot convert parameter 1 from 'const char [2]' to 'Graph<_Ty>::Vertex<_Ty>'
Why this is error , if std::string test = "ABC"; works.
std::string test = "ABC";does implicit casting, but it is not happening while calling the function. TrymyGraph3.AddEdge(std::string("A"),std::string("B"));.Function call overloading by defining another function as in
helps.
The other issue with your code (at least for gcc) is that you are using the same parameter
_Tyin two nested template declarations. The complete, correct code, that works for me is: