我有一个斯坦福大学CS106B正在进行的项目,该项目令人生厌。
我有一个结构dieLocation,它应该表示模具在Boggle板上的位置。
typedef struct dieLocation {
int row, column;
}dieLocation;
然后我有以下代码:
Set<dieLocation> Boggle::getUnmarkedNeighbors(Grid<bool> &markedLocations, dieLocation currentDie) {
int row = currentDie.row;
int column = currentDie.column;
Set<dieLocation> neighbors;
for(int currRow = row - 1; currRow < row + 1; currRow++) {
for(int currCol = column - 1; currCol < column + 1; currCol++) {
//if neighbor is in bounds, get its row and column.
if(markedLocations.inBounds(currRow, currCol)) {
//if neighbor is unmarked, add it to the neighbors set
if(markedLocations.get(currRow, currCol)) {
dieLocation neighbor;
neighbor.row = currRow;
neighbor.column = currCol;
neighbors.add(neighbor);
}
}
}
}
return neighbors;
}
我尝试在Qt Creator中构建此项目,但始终收到错误消息,即:
'operator
我的代码的作用是将传递的dieLocation的行和列分配给它们各自的变量。
然后循环遍历每一行,从比传递的行少一圈到另一行
列也是如此。但是,我相信我正在比较for循环中的整数,但是它表示我正在比较dieLocations?有谁知道为什么会这样?
最佳答案
operator <
用于在Set
内部订购商品。您应该为struct dieLocation
定义它。例如:
inline bool operator <(const dieLocation &lhs, const dieLocation &rhs)
{
if (lhs.row < rhs.row)
return true;
return (lhs.column < rhs.column);
}