我有一个项目,我正在从斯坦福的CS106B开始工作,这是一个令人难以置信的问题。 我有一个struct,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中构建此项目,但我一直收到错误,即: 不匹配&#39;运算符&lt;&#;;(操作数类型是const dieLocation和const dieLocation)
我的代码所做的是,它将传递的dieLocation的行和列分配给它们各自的变量。 然后它循环遍历每一行,从少于传递的行开始,到另一行 列也一样。但是,我相信我在for循环中比较整数,但它说我比较dieLocations?有谁知道为什么会这样?
答案 0 :(得分:1)
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);
}