c++ - 使用 c++0x Lambda 表达式时调用不匹配

标签 c++ lambda c++11

这是有问题的功能:

void Molecule::initSimilarity(int depth)
{
    int m = 1;
    for (int j = 0; j < depth ; j++)
        for (int i = 0 ; i < _size ; i++)   //for any atom A
            if (!getSetMask(                //put all atoms which are equivalnt but not similar to A in their own
                                            //equivalence class
                [&](const Atom& b)->bool {return (_atoms[i]->_class == b._class) && !(isSimilar(*_atoms[i],b));},
                [&m](Atom& b){b._class = m;}     //113
                ).none()) m++;                   //114
}

输出:

In file included from /usr/include/c++/4.5/memory:82:0,

             from /usr/include/boost/dynamic_bitset_fwd.hpp:15,
             from /usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:36,
             from /usr/include/boost/dynamic_bitset.hpp:15,
             from DataStructures/Molecule.h:21,
             from DataStructures/Molecule.cpp:8:

/usr/include/c++/4.5/functional: In static member function ‘static void std::_Function_handler::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Functor = Molecule::initSimilarity(int)::, _ArgTypes = {Atom}]’:

/usr/include/c++/4.5/functional:2103:6: instantiated from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = Molecule::initSimilarity(int)::, _Res = void, _ArgTypes = {Atom}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function::_Useless]’

DataStructures/Molecule.cpp:114:5:
instantiated from here

/usr/include/c++/4.5/functional:1713:9: error: no match for call to

‘(Molecule::initSimilarity(int)::) (Atom)’

DataStructures/Molecule.cpp:113:17: note: candidate is: Molecule::initSimilarity(int)::

我不知道这是怎么发生的,也不知道这到底意味着什么,而且我在 Google 上也找不到任何帮助...

被调用的函数(isSimilar()):

bool Molecule::isSimilar(const Atom &A, const Atom &B)
    {
        AtomSet S;
        for (int i = 0; i < _size; i++)
        {
            if (!_adjecancy[A._index][i]) continue; //Skip any atoms which aren't adjecant to A
            int K = findAtom([&](const Atom& b){return (_adjecancy[B._index][b._index]) && (B._class == b._class) && (!S[B._index]);});
            if (K == -1) return false;
            S.flip(K);
        }
        return true;
    }

以及从中调用的那个:

int Molecule::findAtom(std::function<bool (const Atom)> property, std::function<void (Atom)> action = NULL)
{
    for (int i=0 ; i<_size ; i++)
    {
        if (property(*_atoms[i]))
        {
            if (action != NULL) action(*_atoms[i]);
            return i;
        }
    }
    return -1;
}

使用的类型定义:

typedef dynamic_bitset<> AtomSet;
typedef dynamic_bitset<>::size_type atom_ind;

当然还有错误输出中出现的函数:

AtomSet Molecule::getSetMask(std::function<bool (const Atom)> property, std::function<void (Atom)> action = NULL)
{
    dynamic_bitset<> ret(_size);
    if (property != NULL) for(int i=0; i<_size ; i++) ret.set(i, property(*_atoms[i]));
    return ret;
}

最佳答案

我在 lambda 的参数类型中看到引用,但在 getSetMask 所需的仿函数类型中没有引用。

如果您使这些保持一致,您的错误是否仍然存在?

即如果你想修改Atom,你需要

std::function<void (Atom&)> action

关于c++ - 使用 c++0x Lambda 表达式时调用不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5451435/

相关文章:

c# - 如何在两个字符串数组上使用 Linq 或 lambda 操作获取不匹配位置

python - Python3 中减少错误的 lambda

c++ - 命名空间中所有类的类模板特化

C++17 结构化绑定(bind)和 move 语义

c++ - 使用 cblas_dgemm 计算伪逆的问题

C++11 Lambda 表达式作为回调函数

c++11 - 无法使用 SFINAE (std::enable_if) 覆盖虚函数

c++ - 理解右值引用

c++ - 如何编写可以接受数组或 vector 的函数?

c++ - 无法让 Qt Hello World 工作