### I need help trying to understand graphs

So I am currently using the textbook "C++ Data Structures 3rd Edition", and I have taken some code out of the book. I need help understanding why I keep getting the same three errors. Again the .h and .cpp files are right out of the book. I am merely trying to understand how to make the code work so that I can apply it to my project. Here's the code:

GraphType.h
 ``123456789101112131415161718192021222324252627282930313233`` ``````#ifndef GraphType_H #define GraphType_H #include "QueType.h" template // Assumption: VertexType is a type for which the "=", // "==", and "<<" operators are defined class GraphType { public: GraphType(); // Default of 50 vertices GraphType(int maxV); // maxV <= 50 ~GraphType(); // void MakeEmpty(); // bool IsEmpty() const; // bool IsFull() const; void AddVertex(VertexType); void AddEdge(VertexType, VertexType, int); int WeightIs(VertexType, VertexType); void GetToVertices(VertexType, QueType&); // void ClearMarks(); // void MarkVertex(VertexType); // bool IsMarked(VertexType); private: int numVertices; int maxVertices; VertexType* vertices; int edges[50][50]; bool* marks; // marks[i] is mark for vertices[i]. }; #endif ``````

GraphType.cpp
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100`` ``````#include "GraphType.h" template GraphType::GraphType() // Post: Arrays of size 50 are dynamically allocated for // marks and vertices. numVertices is set to 0; // maxVertices is set to 50. { numVertices = 0; maxVertices = 50; vertices = new VertexType[50]; marks = new bool[50]; } template GraphType::GraphType(int maxV) // Post: Arrays of size maxV are dynamically allocated for // marks and vertices. // numVertices is set to 0; maxVertices is set to maxV. { numVertices = 0; maxVertices = maxV; vertices = new VertexType[maxV]; marks = new bool[maxV]; } template // Post: arrays for vertices and marks have been deallocated. GraphType::~GraphType() { delete [] vertices; delete [] marks; } const int NULL_EDGE = 0; template void GraphType::AddVertex(VertexType vertex) // Post: vertex has been stored in vertices. // Corresponding row and column of edges has been set // to NULL_EDGE. // numVertices has been incremented. { vertices[numVertices] = vertex; for (int index = 0; index < numVertices; index++) { edges[numVertices][index] = NULL_EDGE; edges[index][numVertices] = NULL_EDGE; } numVertices++; } template int IndexIs(VertexType* vertices, VertexType vertex) // Post: Returns the index of vertex in vertices. { int index = 0; while (!(vertex == vertices[index])) index++; return index; } template void GraphType::AddEdge(VertexType fromVertex, VertexType toVertex, int weight) // Post: Edge (fromVertex, toVertex) is stored in edges. { int row; int col; row = IndexIs(vertices, fromVertex); col = IndexIs(vertices, toVertex) edges[row][col] = weight; } template int GraphType::WeightIs (VertexType fromVertex, VertexType toVertex) // Post: Returns the weight associated with the edge // (fromVertex, toVertex). { int row; int col; row = IndexIs(vertices, fromVertex); col = IndexIs(vertices, toVertex); return edges[row][col]; } template void GraphType::GetToVertices(VertexType vertex, QueType& adjVertices) // Post: { int fromIndex; int toIndex; fromIndex = IndexIs(vertices, vertex); for (toIndex = 0; toIndex < numVertices; toIndex++) if (edges[fromIndex][toIndex] != NULL_EDGE) adjVertices.Enqueue(vertices[toIndex]); } ``````

Driver.cpp
 ``12345678910111213141516171819`` ``````#include #include "GraphType.h" using namespace std; int main() { //I am using this driver to test the functions of GraphType GraphType *graph(new GraphType(50)); graph->AddVertex(10); graph->AddVertex(12); graph->AddEdge(10,12,9); system("pause"); return 0; }``````

Error Logs
 ``12345`` ``````Error 4 error LNK1120: 3 unresolved externals C:\Users\Nawgee\Documents\Visual Studio 2010\Projects\graphprac\Debug\graphprac.exe graphprac Error 3 error LNK2019: unresolved external symbol "public: __thiscall GraphType::GraphType(int)" (??0?\$GraphType@H@@QAE@H@Z) referenced in function _main C:\Users\Nawgee\Documents\Visual Studio 2010\Projects\graphprac\graphprac\driver.obj graphprac Error 1 error LNK2019: unresolved external symbol "public: void __thiscall GraphType::AddEdge(int,int,int)" (?AddEdge@?\$GraphType@H@@QAEXHHH@Z) referenced in function _main C:\Users\Nawgee\Documents\Visual Studio 2010\Projects\graphprac\graphprac\driver.obj graphprac Error 2 error LNK2019: unresolved external symbol "public: void __thiscall GraphType::AddVertex(int)" (?AddVertex@?\$GraphType@H@@QAEXH@Z) referenced in function _main C:\Users\Nawgee\Documents\Visual Studio 2010\Projects\graphprac\graphprac\driver.obj graphprac ``````
Could this be related to the compiler needing to see the implementation for template classes in every file it compiles? Maybe try including "GraphType.cpp" instead of the header in driver.cpp
OMG Thanks Maeriden! That worked!

Thanks!
No problemo, but I don't think this is much more than a workaround. I don't have any formal education on templates so you might want to search around the correct way to do this
Topic archived. No new replies allowed.