bool hasWord(int y, int x, string &word) {
if(!inRange(y, x)) return false;
if(board[y][x] != word[0]) return false;
if(word.size() == 1) return true;
for(int direction = 0; direction < 8; direction++) {
int nextX = x + dx[direction];
int nextY = y + dy[direction];
if(hasWord(nextY, nextX, word.substr(1))) // <--- This
return true;
}
return false; }
Run Code Online (Sandbox Code Playgroud)
错误:从类型为 'std::basic_string' 的右值对类型为 'std::string& {aka std::basic_string&}' 的非常量引用进行无效初始化 if(hasWord(nextY, nextX, word.substr(1)) )
为什么我错了?
错误消息说明了一切:从 type 的右值对{aka }类型的非常量引用无效初始化std::stringstd::basic_stringstd::basic_string。
即对非常量的引用不能绑定到临时对象(又名 r 值)。该临时对象是由word.substr(1)表达式创建的。
在声明中:
bool hasWord(int y, int x, string &word)
Run Code Online (Sandbox Code Playgroud)
制作它string const& word,因为您不需要可修改的word.
| 归档时间: |
|
| 查看次数: |
10624 次 |
| 最近记录: |