Search code examples
c++member-functions

private member data not being available to public member function


I have the code below.

When I have main Run() function run the ResetTrackingTable() function. The ResetTrackingTable() calls 0 for my_n_rows and my_n_cols instead of accessing the existing numbers stored in the private member data.

Why is that? It seems like it's making a new instance of the function...


public:

    WordGame();


    void Run(Board &gameBoard, Trie &library, int wordlengthlim);

    void CheckNode(int row, int col, Board & gameBoard, Trie &Library);

    void ExitNode();
    void ResetTrackingTable(int rows, int cols); 

    void PrintWordList();



private:
    std::vector<std::string> my_WordList;
    int my_wordlength; 
    int my_wordlengthlimit;
    std::stringstream currentword; 

    int my_n_cols;
    int my_n_rows;

    std::vector<std::vector<bool>> my_TrackingTable;
};



void WordGame::Run(Board & gameBoard, Trie &Library, int wordlengthlim)
{

    //std::stringstream word;
    //std::string tempword;
    //word.str("");

    currentword.str(""); //Clear current word

    int my_wordlengthlimit = wordlengthlim; //Import limit word length
    int my_wordlength = 0; //Set current word length

    int my_n_rows = gameBoard.numRows(); //Import number of rows
    int my_n_cols = gameBoard.numCols(); //Import number of cols

    for (int i_row = 0; i_row < my_n_rows; ++i_row)
    {
        for (int i_col = 0; i_col < my_n_cols; ++i_col)
        {
            //Actually, when should it be reset?
            this->ResetTrackingTable(); //Initialize the tracking table as all false before each new starting char. 

            CheckNode(i_row, i_col,gameBoard,Library); //Check each beginning node. 
        }
    }
}

void WordGame::ResetTrackingTable()
{

    for (int i_row = 0; i_row < my_n_rows; ++i_row)
    {
        my_TrackingTable.push_back(std::vector<bool> {false});

        for (int i_col = 1; i_col < my_n_cols; ++i_col)
        {
            my_TrackingTable[i_row].push_back(false);  //Initialize the tracking table as all false.
        }
    }

}

Solution

  • These lines of code:

    int my_n_rows = gameBoard.numRows(); //Import number of rows
    int my_n_cols = gameBoard.numCols(); //Import number of cols
    

    are declaring new variables inside the Run function.

    If you want to refer to the member variables instead, drop the int declarator:

    my_n_rows = gameBoard.numRows(); //Import number of rows
    my_n_cols = gameBoard.numCols(); //Import number of cols
    

    You need to do this for all your member variables that you want to use.

    Also, your declaration:

    void ResetTrackingTable(int rows, int cols); 
    

    doesn't match its definition:

    void WordGame::ResetTrackingTable() { // ...
    

    you need to have the same number of parameters of the same type.