I am working on Graphs and I created a Graph class and added #include <queue> to my code. If I write queue<int> MyQueue in main function it works well. But if I write the same code queue<int> MyQueue in my class Graph it gives me a runtime error. Waiting for your help.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class AdjListImp_Graph
{
public:
vector<int> *array[8];
vector<int> List[8];
AdjListImp_Graph(){
for(int i=1; i<8; i++)
array[i] = &List[i];
}
};
class AdjMatrixImp_Graph
{
public:
int AdjMatrix[7][7];
queue<int> MyList; // !Having problem with this code!
AdjMatrixImp_Graph(){
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
AdjMatrix[i][j] = 0;
}
};
int main (void)
{
AdjListImp_Graph MyGraph_3a, MyGraph_3b, MyGraph_3c;
MyGraph_3a.List[1].push_back(4);
MyGraph_3a.List[2].push_back(4);
MyGraph_3a.List[4].push_back(7);
MyGraph_3a.List[6].push_back(3);
MyGraph_3a.List[7].push_back(5);
MyGraph_3b.List[1].push_back(2);
MyGraph_3b.List[2].push_back(5); MyGraph_3b.List[2].push_back(7);
MyGraph_3b.List[4].push_back(3); MyGraph_3b.List[4].push_back(6);
MyGraph_3b.List[5].push_back(4);
MyGraph_3b.List[6].push_back(1); MyGraph_3b.List[6].push_back(7);
MyGraph_3c.List[2].push_back(1);
MyGraph_3c.List[3].push_back(4); MyGraph_3c.List[3].push_back(6);
MyGraph_3c.List[4].push_back(5);
MyGraph_3c.List[5].push_back(2);
MyGraph_3c.List[6].push_back(7);
MyGraph_3c.List[7].push_back(2);
/********************************************************************************************************/
AdjMatrixImp_Graph My_Graph_3a, My_Graph_3b, My_Graph_3c;
My_Graph_3a.AdjMatrix[1][4] = 1;
My_Graph_3a.AdjMatrix[2][4] = 1;
My_Graph_3a.AdjMatrix[3][2] = 1;
My_Graph_3a.AdjMatrix[4][7] = 1; // 4'ten 7'ye yol var.
My_Graph_3a.AdjMatrix[6][3] = 1;
My_Graph_3a.AdjMatrix[7][5] = 1;
My_Graph_3b.AdjMatrix[1][2] = 1;
My_Graph_3b.AdjMatrix[2][5] = 1; My_Graph_3b.AdjMatrix[2][7] = 1;
My_Graph_3b.AdjMatrix[4][3] = 1; My_Graph_3b.AdjMatrix[4][6] = 1;
My_Graph_3b.AdjMatrix[5][4] = 1;
My_Graph_3b.AdjMatrix[6][1] = 1; My_Graph_3b.AdjMatrix[6][7] = 1;
My_Graph_3c.AdjMatrix[2][1] = 1;
My_Graph_3c.AdjMatrix[3][4] = 1; My_Graph_3c.AdjMatrix[3][6] = 1;
My_Graph_3c.AdjMatrix[4][5] = 1;
My_Graph_3c.AdjMatrix[5][2] = 1;
My_Graph_3c.AdjMatrix[6][7] = 1;
My_Graph_3c.AdjMatrix[7][2] = 1;
system("pause");
return EXIT_SUCCESS;
}
In your initialization of
AdjMatrixImp_Graph::AdjMatrixin the constructor ofAdjMatrixImp_Graph, you are going beyond the array boundary. It possibly writes into the memory area ofAdjMatrixImp_Graph::MyList, and corrupts its structure.Change the loops toOr you can usefor (int i;i<7;i++) for(int j;j<7;j++){....memset(AdjMatrix, 0, sizeof(AdjMatrix));Checking the
maincode suggests that the definition ofAdjMatrixshould be changed toAdjMatrix[8][8].