I am trying to create a generic "Graph" class to create digraphs for flows, etc. I got the idea from one of my Data Structs books, but am having some problems with the syntax (I think it's the syntax).
I have a .cpp file that has int main() in it, and I #include a graph.h file which houses the template for the graph class. I then have all of the functions for that template class in the file graph.cpp (which #includes graph.h). AM I doing this incorrectly?
Here is my code so far:
Code:graph.h #ifndef GRAPH_H #define GRAPH_H #include <stdlib.h> #include <assert.h> #include "edge.h" template <class Item> class Graph { public: static const int MAX_VERTS = 50; Graph() {num_verts = 0;} void add_vertex(const Item& label); void add_edge(int source, int target, int capacity); void init_flows(); void update_flow(int flow_val); void remove_edge(int source, int target); Item& operator [] (int vertex); int size() const {return num_verts;} bool is_edge(int source, int target) const; private: Edge edges[MAX_VERTS][MAX_VERTS]; Item labels[MAX_VERTS]; int num_verts; }; #endifCode:graph.cpp #include "graph.h" #include "edge.h" #include <assert.h> template <class Item> void Graph<Item>::add_vertex(const Item& label) { int new_vertex; int i; assert(size() < MAX_VERTS); new_vertex = num_verts; num_verts++; for(i = 0; i < num_verts; i++) { edges[i][new_vertex].exists = false; edges[new_vertex][i].exists = false; } labels[new_vertex] = label; } template <class Item> void Graph<Item>::add_edge(int source, int target, int capacity) { assert(source < size()); assert(target < size()); edges[source][target].exists = true; edges[source][target].capacity = capacity; }; template <class Item> void Graph<Item>::init_flows() { //I will code this later } template <class Item> void Graph<Item>::update_flow(int flow_val) { //I will code this later too } template <class Item> void Graph<Item>::remove_edge(int source, int target) { assert(source < size()); assert(target < size()); edges[source][target].exists = false; edges[source][target].capacity = 0; edges[source][target].flow = 0; } template <class Item> Item& Graph<Item>::operator [] (int vertex) { assert(vertex < size()); return labels[vertex]; } template <class Item> bool Graph<Item>::is_edge(int source, int terget) const { assert(source < size()); assert(target < size()); return edges[source][target].exists; }Any help would be greatly appreciated.Code:edge.h #ifndef EDGE_H #define EDGE_H class Edge { public: bool exists; int capacity; int flow; Edge() {exists = false; capacity = 0; flow = 0;} }; #endif
LilShieste
Changed from quote tags to Code Tags by Kermi3



LinkBack URL
About LinkBacks


