kmetin
kmetin

Reputation: 263

I can't use Queue in my abstract data type

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;
}

Upvotes: 1

Views: 231

Answers (2)

Igor
Igor

Reputation: 27248

Change the declaration

    int AdjMatrix[7][7];

to

    int AdjMatrix[8][8];

and it won't crash. When you have queue in main, it works by coincidence.


By the way, I strongly suggest you to change the design of your data structures and not to use pointers to containers.

Upvotes: 1

fefe
fefe

Reputation: 3432

In your initialization of AdjMatrixImp_Graph::AdjMatrix in the constructor of AdjMatrixImp_Graph, you are going beyond the array boundary. It possibly writes into the memory area of AdjMatrixImp_Graph::MyList, and corrupts its structure.

Change the loops to for (int i;i<7;i++) for(int j;j<7;j++){.... Or you can use memset(AdjMatrix, 0, sizeof(AdjMatrix));

Checking the main code suggests that the definition of AdjMatrix should be changed to AdjMatrix[8][8].

Upvotes: 0

Related Questions