Мне нужно распечатать матрицу смежности графа (2d-массив) в основном, массив называется ребра и создан с использованием указателей в файле TransportGraph. cpp. Я пытался l oop через размер, но возвращает ошибку. что я сделал не так?
Main. cpp:
#include <iostream>
#include <cstdio>
#include "TransportGraph.h"
#include "TransportGraph.cpp"
using namespace std;
int main()
{
TransportGraphType<string> newGraph;
//adding vertexes
string dh= "Dhaka", ran="Rangpur", raj="Rajshahi", my = "Mymensingh", sy="Sylhet", ch="Chittagong", br="Barisal", kh= "Khulna";
newGraph.AddVertex(dh);
newGraph.AddVertex(ran);
//added all the other relevant vertexes
//adding edges with weight 1 as they are undirected.
newGraph.AddEdge(dh,raj,1);
newGraph.AddEdge(raj,dh,1);
//added the rest similarly, it is a bidirectional graph
//my attempt at printing out the adjacency matrix:
for (int i=0;j<newGraph.edges.size();i++){
cout<<newGraph.edges[i]<<endl;
}
}
Файл TransportGraph. cpp: (имеет массив ребер и другие соответствующие методы)
#include "TransportGraph.h"
#include "stacktype.cpp"
#include "quetype.cpp"
#include <iostream>
using namespace std;
const int NULL_EDGE = 0;
template<class VertexType>
TransportGraphType<VertexType>::TransportGraphType()
{
numVertices = 0;
maxVertices = 50;
vertices = new VertexType[50];
edges = new int*[50];
for(int i=0;i<50;i++)
edges[i] = new int [50];
marks = new bool[50];
}
template<class VertexType>
TransportGraphType<VertexType>::TransportGraphType(int maxV)
{
numVertices = 0;
maxVertices = maxV;
vertices = new VertexType[maxV];
edges = new int*[maxV];
for(int i=0;i<maxV;i++)
edges[i] = new int [maxV];
marks = new bool[maxV];
}
template<class VertexType>
TransportGraphType<VertexType>::~TransportGraphType()
{
delete [] vertices;
delete [] marks;
for(int i=0;i<maxVertices;i++)
delete [] edges[i];
delete [] edges;
}
//has other AddVertex and AddEdge methods, also BFS DFS methods
Файл TransportGraph.h (содержит методы в классе):
#ifndef TRANSPORTGRAPH_H_INCLUDED
#define TRANSPORTGRAPH_H_INCLUDED
#include "stacktype.h"
#include "quetype.h"
template<class VertexType>
class TransportGraphType{
public:
TransportGraphType();
TransportGraphType(int maxV);
~TransportGraphType();
void MakeEmpty();
bool IsEmpty();
bool IsFull();
void AddVertex(VertexType);
void AddEdge(VertexType,VertexType, int);
int WeightIs(VertexType,VertexType);
void GetToVertices(VertexType,QueType<VertexType>&);
void ClearMarks();
void MarkVertex(VertexType);
bool IsMarked(VertexType);
void DepthFirstSearch(VertexType,VertexType);
void BreadthFirstSearch(VertexType,VertexType);
private:
int numVertices;
int maxVertices;
VertexType* vertices;
int **edges;
bool* marks;
};
#endif // TRANSPORTGRAPH_H_INCLUDED