public 成员函数无法使用私人成员数据
private member data not being available to public member function
我有下面的代码。
当我有 main 运行() 函数时 运行 ResetTrackingTable() 函数。 ResetTrackingTable() 为 my_n_rows 和 my_n_cols 调用 0,而不是访问存储在私有成员数据中的现有号码。
这是为什么?它似乎正在创建函数的新实例...
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() { // ...
您需要具有相同数量的相同类型的参数。
我有下面的代码。
当我有 main 运行() 函数时 运行 ResetTrackingTable() 函数。 ResetTrackingTable() 为 my_n_rows 和 my_n_cols 调用 0,而不是访问存储在私有成员数据中的现有号码。
这是为什么?它似乎正在创建函数的新实例...
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() { // ...
您需要具有相同数量的相同类型的参数。