2

I am trying to populate a vector which is a member to an object's class within the constructor. I received a vector index out of bounds error at runtime. I then stepped into the program while it was running using the debugger to see why this error came up. Apparently the vector never was initialized even though i have done so in the constructor. I have my code snippets below, I have added comments to make it more understandable. Any help is greatly appreciated.

Here is the maze.h code snippet

class maze { public: int numlevels, numrows, numcols; int startlevel, startrow, startcol, endlevel, endrow, endcol; int level, row, col; // here is the construtor declaration maze(int l, int m, int n, int o, int p, int q, int r, int s, int t); ~maze(); //these two lines can be ignored void solve(int startlevel, int startrow, int startcol, int endlevel, int endrow, int endcol); queue<char> q; //I declare the 3d array here as a public member vector<vector<vector<string> > > mazeGraph; }; 

here is the maze.cpp code snippet

//here is the constructor body maze::maze(int l, int m, int n, int o, int p, int q, int r, int s, int t) { numlevels = l; numrows = m; numcols = n; startlevel = 0; startrow = p; startcol = q; endlevel = r; endrow = s; endcol = t; //ignore the redundancy of these assignments for now. //i try to initialize the array here vector<vector<vector<string> > > mazeGraph(numlevels, vector<vector<string> >(numrows, vector<string>(numcols, "0"))); } 

here is the part of the source .cpp where the error occurs

for (f = 0; f < numMazes; i++) { //these variables are read from a text file and are used to initialize an object's vector member. //nothing is wrong with the file reading, these variables are updated correctly infile >> numlevels >> numrows >> numcols; infile >> startlevel >> startrow >> startcol; infile >> endlevel >> endrow >> endcol; //mazeList is an array of maze objects. each maze object has its own 3 dimensional array which i am trying to initialize with the above variables. mazeList.push_back(maze(numlevels,numrows,numcols,startlevel,startrow,startcol,endlevel,endrow,endcol)); //mazeList.at(f).numlevels refers to the numlevels member belonging to the fth maze object in the list of mazes. //this is the same for numrows and numcols //these member variables are updated corretly for (i = 0; i < mazeList.at(f).numlevels; i++) { for (j = 0; j < mazeList.at(f).numrows; j++) { for (k = 0; k < mazeList.at(f).numcols; k++) { //the text file contains strings that are to be put into a 3-d array //mazeGraph is the 3-d vector belonging to the current maze object //mazeList.at(f).mazeGraph refers to the mazeGraph belonging to the current maze in the list. infile >> mazeList.at(f).mazeGraph[i][j][k]; //the above line generates the error } } } 
4
  • 2
    You re-declared it in your constructor which hides your member variable. Which is odd because you correctly initialized all your other member variables, I'm not sure why you felt the need to add the type for maze_graph (which turned it into a declaration) Commented Nov 29, 2016 at 20:18
  • 2
    for (f = 0; f < numMazes; i++) -- You do see that there is an issue here? Commented Nov 29, 2016 at 20:23
  • woops, I did not see that @PaulMcKenzie Thanks! Commented Nov 29, 2016 at 20:29
  • mazeGraph(numlevels,(numrows,(numcols, "0"))); why wouldn't this work? Commented Nov 29, 2016 at 20:32

1 Answer 1

1

The local declaration

vector<vector<vector<string> > > mazeGraph(numlevels, vector<vector<string> >(numrows, vector<string>(numcols, "0"))) 

in the constructor, just declares a local vector called mazeGraph. Which has no relation to the data member called mazeGraph.

To initialize the member you can use a constructor initializer list, or you can assign to it.


Example for a simpler situation with the same problem:

struct S { int x; S() { int x = 666; } //! Ungood, just a local declaration! }; struct T { int x; T(): x{42} {} // OK, initializes data member `x`. }; struct U { int x; U(){ x = 42; } // Also OK, not ideal but initializes member `x`. }; 

For both efficiency and simplicity it can be a good idea to implement a 3D array in terms of a single underlying vector providing the storage. Just provide an indexing function that internally computes which 1D item to access for a given 3D indices.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.