Template problem

This is a discussion on Template problem within the C++ Programming forums, part of the General Programming Boards category; I am trying to create a generic "Graph" class to create digraphs for flows, etc. I got the idea from ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    Template problem

    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;
    };
    
    #endif
    Code:
    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;
    }
    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
    Any help would be greatly appreciated.

    LilShieste

    Changed from quote tags to Code Tags by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Hi, an welcome to these boards.

    Nice try using to quote tags on your post, but code tags are much esier to read. For information on them I suggest you read this.

    If there's any questions about these forums that I can answer, or can help with please feel free to let me know.

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    In general, include everything in the .h file. There are other solution including #include xyz.cpp at the end of the .h file. Nonetheless, I recommend that you start out simple.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 05:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  5. Problem with template usage
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2002, 06:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21