C ++ find_if找不到谓词

Tom*_*Tom 2 c++ algorithm vector

我收到错误消息:

no matching function for call to ‘findByPosition::findByPosition(std::vector<int>::size_type&, std::vector<int>::size_type&)’
Run Code Online (Sandbox Code Playgroud)

当我投ikint我得到:

no matching function for call to ‘findByPosition::findByPosition(int, int)’
Run Code Online (Sandbox Code Playgroud)

我不知道我的谓词有什么问题。我已经()根据需要重载了运算符:

struct findByPosition
{
    const Node needle;
    findByPosition(const Node& sought) : needle(sought) {}
    bool operator()(int i,int j) const
    {
        return ((needle.i == i) && (needle.j == j));

    }
};

SparseMatrix& SparseMatrix::operator*=(const SparseMatrix &other)
{
    SparseMatrix SparseMatrixResult(_numRow, other._numCol);
    vector<Node>::iterator rowMulti, colMulti;

    if(_numCol != other._numRow)
    {
        // error multiplying
    }

    for(std::vector<int>::size_type i = 0; i != (unsigned int)_numRow; i++) {

            for(std::vector<int>::size_type j = 0; j != (unsigned int)_numCol; j++) {

                for(std::vector<int>::size_type k = 0; k != (unsigned int)_numCol; k++)
                {
                    rowMulti = find_if(_matrix.begin(), _matrix.end(), findByPosition(i,k));
                }
            }
        }

    *this = SparseMatrixResult;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

_matrix 类型:

vector<Node> _matrix;
Run Code Online (Sandbox Code Playgroud)

Kar*_*k T 5

在调用时,findByPosition(i,k)您实际上是在尝试调用构造函数,而不是operator()

您需要定义一个构造函数,然后可以在该行使用它来生成一个对象find_if然后在theobject(i,j) 内部调用以调用operator()

您可以从错误中看到这一点

没有匹配的函数可以调用'findByPosition :: findByPosition(int,int)'

它试图找到构造函数

要正确使用谓词,您实际上需要翻转运算符和构造函数。构造函数应采用i,j对函子的所有调用通用的构造函数,而运算符应引用,const Node&因为这是矩阵的元素类型,并且是函函将被调用的数据类型。

struct findByPosition
{
    findByPosition(int _i, int _j) : i(_i), j(_j) {}
    bool operator()(const Node& needle) const
    {
        return ((needle.i == i) && (needle.j == j));

    }
private:
    int i,j;
};
Run Code Online (Sandbox Code Playgroud)

这样,构造函数将使用保存的构造一个对象ij然后将其传递给您的find_if函数。