【问题标题】:no match for call when using c++0x Lambda expressions使用 c++0x Lambda 表达式时调用不匹配
【发布时间】:2011-07-24 00:18:46
【问题描述】:

这是有问题的函数:

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
}

还有输出:

在 /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: 在 静态成员函数'静态无效 std::_Function_handler::_M_invoke(const std::_Any_data&, _ArgTypes ...) [与 _Functor = 分子::initSimilarity(int)::, _ArgTypes = {Atom}]':

/usr/include/c++/4.5/functional:2103:6: 实例化自 'std::function<_res ...>::function(_Functor, typename std::enable_if::value), std::function<_res ...>::_Useless>::type) [with _Functor = 分子::initSimilarity(int)::, _Res = void,_ArgTypes = {Atom},类型名 std::enable_if::value), std::function<_res ...>::_无用>::type = std::function::_Useless]'

DataStructures/Molecule.cpp:114:5:
从这里实例化

/usr/include/c++/4.5/functional:1713:9: 错误:调用不匹配

‘(分子::initSimilarity(int)::) (原子)'

数据结构/分子.cpp:113:17: 注:候选人是: 分子::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;
}

【问题讨论】:

  • 您将需要显示您正在调用的函数的完整签名,包括它所依赖的任何类型定义。
  • 您好,我添加了任何我认为可能相关的内容,感谢您的帮助。
  • 对不起,我的意思是getSetMask,我应该第一次说。
  • Doh,添加了所有错误中提到的函数。

标签: c++ lambda c++11


【解决方案1】:

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

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

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

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

【讨论】:

  • 这似乎有效,谢谢!您是否有机会详细说明错误来源,或者向我推荐一些可读的材料?由于此功能相当新,因此阅读材料很少。谢谢!
  • @Shai:lambdas 在 C++0x 中可能是新的,但错误消息不是。 “错误:调用不匹配”表示“参数类型不匹配”(或“参数数量错误”),我相信即使您编写了自己的仿函数,您也会收到相同的消息。当然,错误消息的上下文部分比较混乱,因为函子没有名称,但故障排除方法没有改变。
猜你喜欢
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 2014-09-20
相关资源
最近更新 更多