我正在编写一些代码,以文本文件的形式解决填字游戏。解析文件并创建类型vector<vector<char>>
的填字游戏可以正常工作。索引填字游戏EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
时,在horizontal_forward_search
函数中遇到crossword[row_num][col_num]
错误。
pair<int, int> iterate(vector<vector<char>> crossword, string word) {
int row_num = 0;
for (vector<char> row: crossword) {
++row_num;
int col_num = 0;
for (char letter: row) {
++col_num;
if (letter == word[0]) { // first letter of word found, confirm/deny word
pair<int, int> location = horizontal_forward_search(crossword, row_num, col_num, word);
if (location.first != -1)
return location;
}
}
}
return make_pair(-1, -1);
}
和
pair<int, int> horizontal_forward_search(vector<vector<char>> crossword, int row_num, int col_num, string word) {
long remaining_chars = crossword[row_num].size() - col_num;
if (word.size() > remaining_chars) // word size exceeds remaining characters
return make_pair(-1, -1);
else {
string temp_str = "";
int cur_pos = col_num;
int i = 0;
while (i < word.size()) {
char cur_letter = crossword[row_num][col_num]; // Error here
temp_str.push_back(cur_letter);
++cur_pos; // next pos in array
++i;
}
if (temp_str == word)
return make_pair(row_num, col_num);
}
return make_pair(-1,-1);
}
我知道有几种方法可以对此进行调试,但是代码很简单。在iterate
函数中,crossword
被复制到crossword
函数中的horizontal_forward_search
中,理想情况下可以访问填字游戏。但是,在执行crossword[row_num][col_num]
时,我遇到了一个错误。