【问题标题】:Getting no match for 'operator<' error. C++无法匹配 'operator<' 错误。 C++
【发布时间】:2015-04-14 00:54:50
【问题描述】:

我正在使用斯坦福的 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?有谁知道为什么会这样?

【问题讨论】:

  • 如果Setstd::set 类似,则为有序。
  • Set 可能使用operator&lt; 对其元素进行排序。如果是这样,请在 dieLocation 上定义 operator&lt; 重载。

标签: c++ qt for-loop match operand


【解决方案1】:

operator &lt; 用于您的Set 内部,用于订购商品。您应该为struct dieLocation 定义它。例如:

inline bool operator <(const dieLocation &lhs, const dieLocation &rhs)
{
    if (lhs.row < rhs.row)
        return true;
    return (lhs.column < rhs.column);
}

【讨论】:

    猜你喜欢
    • 2011-12-10
    • 2013-07-31
    • 1970-01-01
    • 2011-10-06
    • 2011-12-08
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多