0

Hi everybody I am currently trying to write a recursive member function inside of a class which uses a private data member to call itself again. I have not been able to figure out why I can't call it recursively and go to this block in the vector.h file instead of recalling the function. I assume its to do with the range but while debugging I can clearly see that such a member exists.

reference
  at(size_type __n)
  {
_M_range_check(__n);
return (*this)[__n];
  }

My code is as follows:

void generateMaze(bool seedSet,cell currentCell){
        //this should work in theory need to debug.
        int x;
        int y;
        if(!seedSet){
            seed = rand() % 4;
        } 
        //currentCellNeedsToBePassedByReference
        //FigureOutWhatCurrentCell Should be
        currentCell.setVisited(true);
        currentCell.getPosition(x,y);
        //This whole if else chain is a way of picking a random valid direction.
        //The problem is we try to access nonexisting elements.
        if((x-1>=0 && y>=0 && x-1<=columns-1 && y<=rows-1) && seed%4==0){
            if(!cellVector.at(y).at(x-1).checkVisited()){
                currentCell.setWestWall(false);
                cellVector.at(y).at(x).setWestWall(false);
                cellVector.at(y).at(x-1).setEastWall(false);
                mazeVisual.at(y*2+1).at(x*4) = ' ';
                generateMaze(seedSet,cellVector.at(y).at(x-1));
            }
        } else
        if((x>=0 && y-1>=0 && x<=columns-1 && y-1<=rows-1) && seed%4==1){
            if(!cellVector.at(y-1).at(x).checkVisited()){
                currentCell.setNorthWall(false);
                cellVector.at(y).at(x).setNorthWall(false);
                cellVector.at(y-1).at(x).setSouthWall(false);
                mazeVisual.at(y*2).at(x*4+1) = ' ';
                mazeVisual.at(y*2).at(x*4+2) = ' ';
                mazeVisual.at(y*2).at(x*4+3) = ' ';
                generateMaze(seedSet,cellVector.at(y-1).at(x));
            }
        } else
        if((x+1<=columns-1 && y<=rows-1 && x+1 >= 0 && y >= 0) && seed%4==2){
            if(!cellVector.at(y).at(x+1).checkVisited()){
                currentCell.setEastWall(false);
                cellVector.at(y).at(x).setEastWall(false);
                cellVector.at(y).at(x+1).setWestWall(false);
                mazeVisual.at(y*2+1).at(x*4+4) = ' ';
                generateMaze(seedSet,cellVector.at(y).at(x+1));
            }
        } else
        if((x<=columns && y+1<=rows && x >= 0 && y+1 >= 0) && seed%4==3){
            if(!cellVector.at(y+1).at(x).checkVisited()){
                currentCell.setSouthWall(false);
                cellVector.at(y).at(x).setSouthWall(false);
                cellVector.at(y+1).at(x).setNorthWall(false);
                mazeVisual.at(y*2+2).at(x*4+1) = ' ';
                mazeVisual.at(y*2+2).at(x*4+2) = ' ';
                mazeVisual.at(y*2+2).at(x*4+3) = ' ';
                std::cout << cellVector.at(y+1).at(x).checkVisited();
                //this is the problem spot, the function cant be called for some reason.
                generateMaze(seedSet,cellVector.at(y+1).at(x));
            }
        }
        return;
    }

I know that it doesn't look great but Ihope you can help I just can't understand why it says vector is out of range when in the debug it shows that I have a cellVector filled with cell instances. I added lots of input checks and make sure that cellvector is not empty and has the structure of a 2d array with sizes [1][1]. I hope you can help. Thank you.

  • 1
    First of all please try to create a [mre] to show us, with emphasis on the *minimal* part. Then when you debug, what are the sizes of the vectors you use? What are the indexes you are trying to use? – Some programmer dude Oct 17 '22 at 07:00
  • As a possible hint: Is `rows` the number of elements in the vector? If it is, will the check `y+1<=rows` then be correct? – Some programmer dude Oct 17 '22 at 07:03
  • You pass the cell by value to generateMaze and (depending on cell class design) it may mean that none of your logic works or worse. – Öö Tiib Oct 17 '22 at 07:03
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 17 '22 at 07:16
  • 1
    You have a comment `currentCellNeedsToBePassedByReference` but `currentCell` isn't passed by reference – Alan Birtles Oct 17 '22 at 07:17
  • hi so I solved the problem it seems that c++ complains when I try too use an rvalue to pass by reference so what I did was create a temp instance of the class and pass that in the recursive and since I only use that temp as a gps to see where we are and dont directly manipulate it, it worked. – tommyBruiser Oct 17 '22 at 14:58
  • I still can't figure out how to pass by reference using an element of a vector, I don't think it's possible. Regarding your questions about the logic and the rows, they were all working after I fixed this issue. Thank you everyone. – tommyBruiser Oct 17 '22 at 14:59
  • There should be no problem passing an element by reference, show a [mre] of the problem you're having (maybe in a new question) – Alan Birtles Oct 18 '22 at 06:50

0 Answers0