简体   繁体   English

没有匹配到'operator <'错误。 C ++

[英]Getting no match for 'operator<' error. C++

I have a project I'm working on from stanford's CS106B, which is boggle. 我有一个斯坦福大学CS106B正在进行的项目,该项目令人生厌。 I have a struct, dieLocation, which is supposed to represent the location of a die on my Boggle board. 我有一个结构dieLocation,它应该表示模具在Boggle板上的位置。

typedef struct dieLocation {
    int row, column; 
}dieLocation;

I then have this code: 然后我有以下代码:

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;
}

I try to build this project in Qt Creator, but I keep receiving an error, which is: no match for 'operator<'(operand types are const dieLocation and const dieLocation) 我尝试在Qt Creator中构建此项目,但我不断收到错误,即:'operator <'不匹配(操作数类型为const dieLocation和const dieLocation)

What my code does, is it assigns the row and column of the passed dieLocation to their respective variables. 我的代码的作用是将传递的dieLocation的行和列分配给它们各自的变量。 it then loops through each row, starting from one less than the passed row, to one more the same goes for columns. 然后,它循环遍历每一行,从比所传递的行少一圈,再到每一列相同。 however, I believe I am comparing integers in the for loop, but it says i'm comparing dieLocations? 但是,我相信我正在比较for循环中的整数,但是它表示我正在比较dieLocations? Does anyone understand why this happens? 有谁知道为什么会这样?

operator < is used inside of your Set for ordering it's items. operator <用于Set中的项目订购。 You should define it for struct dieLocation . 您应该为struct dieLocation定义它。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM