与'operator<'错误不匹配。C++

Getting no match for 'operator<' error. C++

本文关键字:C++ 不匹配 operator lt 错误      更新时间:2023-10-16

我有一个项目正在斯坦福大学的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和常量dieLocation)

我的代码所做的是,它将传递的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);
}