Reputation: 1672
Hello I have a getter in adjacencyMatrix class
int AdjacencyMatrix::getVertexFirst() const { return vertexFirst; }
and constructor
AdjacencyMatrix::AdjacencyMatrix() {
this->vertexCount=0;
this->vertexFirst=0;
this->edgeCount=0;
this->matrix=0;
this->wage=0;
}
bool AdjacencyMatrix::createFromFile(string path) {
fstream file;
file.open(path.c_str(), fstream::in);
if (file.good())
{
int vertexF,vertexE,wag;
file >> this->edgeCount;
file >> this->vertexCount;
file >> this->vertexFirst;
matrix = new int *[vertexCount];
wage = new int *[vertexCount];
for (int i = 0; i < vertexCount; i++)
{
matrix[i]=new int[vertexCount];
wage[i]=new int[vertexCount];
}
//fill matrix by zeros
for (int i = 0; i < vertexCount; i++)
{
for(int j=0; j<vertexCount;j++)
{
matrix[i][j]=0;
wage[i][j]=0;
}
}
// fill matrix by 1
for(int i=0; i<edgeCount; i++)
{
file >> vertexF >> vertexE >> wag;
this->matrix[vertexF][vertexE]=1;
this->wage[vertexF][vertexE]=wag;
}
file.close();
return true;
}
return false;
}
of course print works in Adjacency class And now I want to have this value in Dijkstra class
//Dijkstra.cpp
#include "Dijkstra.h"
AdjacencyMatrix am;
bool Dijkstra::makeDijkstraAlgo() {
int vertexCount=am.getVertexCount();
int vertexFirst=am.getVertexFirst();
int **wage=am.getWage();
cout << vertexCount;
cout << vertexFirst;
.......... }
this is my main class
#include <iostream>
#include "Dijkstra.h"
#include "Screen.h"
using namespace std;
int main() {
AdjacencyMatrix am;
Dijkstra dijkstra;
am.createFromFile("matrix.txt");
dijkstra.makeDijkstraAlgo();
dijkstra.viewDijkstra();
return 0;
}
and this cout show only 0, but in AdjacencyMatrix show normal value. Can you help me ?
UPDATE
I notice that always will be 0 because I initialized value in constructor.... So How to make something like this
I create a matrix from file and add value to vertexCount etc.
am.createFromFile("matrix.txt");
now I want to get this value(vertexCount etc.) from adjacency matrix class to Dijkstry class and make
dijkstra.makeDijkstraAlgo();
dijkstra.viewDijkstra();
How can I solve it ?
Upvotes: 0
Views: 210
Reputation: 66371
You're creating one matrix but using another.
makeDijkstraAlgo
uses the global matrix called ”am”, but main
has its own matrix by the same name.
Get rid of the global and pass main
's matrix to the function
bool Dijkstra::makeDijkstraAlgo(const AdjacencyMatrix& am) {
int vertexCount=am.getVertexCount();
int vertexFirst=am.getVertexFirst();
int **wage=am.getWage();
cout << vertexCount;
cout << vertexFirst;
// ...
}
int main() {
AdjacencyMatrix am;
Dijkstra dijkstra;
am.createFromFile("matrix.txt");
dijkstra.makeDijkstraAlgo(am);
dijkstra.viewDijkstra();
return 0;
}
Upvotes: 1