私有成员数据不可用于公共成员功能



我有下面的代码。

当我有主 Run(( 函数运行 ResetTrackingTable(( 函数时。 ResetTrackingTable(( 调用 0 表示my_n_rows和my_n_cols,而不是访问存储在私有成员数据中的现有号码。

为什么?似乎它正在制作函数的新实例......


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

这些代码行:

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

Run函数中声明新变量。

如果要改为引用成员变量,请删除int声明符:

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

您需要对要使用的所有成员变量执行此操作。

此外,您的声明:

void ResetTrackingTable(int rows, int cols); 

与其定义不匹配:

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

您需要具有相同数量的相同类型的参数。

最新更新