Header file is “graph.h”
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <map>
#include <vector>
using namespace std;
template <class T>
class VERTEX
{
public:
VERTEX(T inVertex): m_vertex(inVertex), m_visited(false){}
~VERTEX(){}
private:
T m_vertex;
bool m_visited;
};
template <class T>
class GRAPH
{
public:
GRAPH() {}
~GRAPH(){}
typedef VERTEX<T> GRAPHVERTEX;
typedef vector<GRAPHVERTEX> ADJLIST;
typedef map<GRAPHVERTEX, ADJLIST> GRAPHMAP;
void insert(GRAPHVERTEX inSRC, GRAPHVERTEX inDST)
{
GRAPHMAP::iterator itr = m_graph.find(inSRC);
}
private:
GRAPHMAP m_graph;
};
#endif
And test file is
#include "graph.h"
int main( int argc, char**argv)
{
GRAPH<int> *G = new GRAPH<int>();
G->insert(VERTEX<int>(0), VERTEX<int>(2));
return 0;
}
There are two problems.
First you have to qualify the dependent type in
insert:Second, you need a < operator for your vertex class. In the public section of VERTEX add this:
As a matter of style note that in C++ ALL CAPS names are usually reserved for constants.
Vertexwould be a much more normal name for your class. Also note that havingusing namespacein a header can have many undesired and unpredictable results depending on include order and should be completely avoided.EDIT: At least when I compiled this with g++ the first error I got was regarding GRAPHMAP::iterator. When a compiler sees an identifier that could be treated as a variable or a type, it choose to interpret it as a variable by default, but then at a later point discovered it was actually a type. You tell the compiler that it’s really a type by using the
typenamekeyword.The second thing to note is that
mapis an ordered container and as such you need to either pass in a comparison function OR provide a < operator for the key of themap. SinceVERTEXis the map key, I set up anoperator<so that the objects can be sorted and have an order maintained. You may need to adjust the comparison operator as yourVERTEXclass evolves.