使用 c++0x Lambda 表达式时调用不匹配
这是有问题的函数:
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 包含的文件中,
来自 /usr/include/boost/dynamic_bitset_fwd.hpp:15, 来自/usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:36, 来自/usr/include/boost/dynamic_bitset.hpp:15, 来自 DataStructures/Molecule.h:21, 来自 DataStructures/Molecule.cpp:8:
/usr/include/c++/4.5/function: 在 静态成员函数'静态无效 std::_Function_handler::_M_invoke(const std::_Any_data&, _ArgTypes ...) [与 _Functor = 分子::initSimilarity(int)::, _ArgTypes = {Atom}]':
/usr/include/c++/4.5/function:2103:6: 实例化自 'std::函数<_Res(_ArgTypes ...)>::function(_Functor, 类型名 std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes) ...)>::_Useless>::type) [与 _Functor = 分子::initSimilarity(int)::, _Res = void,_ArgTypes = {Atom},类型名 std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes) ...)>::_无用>::类型= std::function::_Useless]'
DataStructures/Molecule.cpp:114:5:
从这里实例化/usr/include/c++/4.5/function:1713:9: 错误:与调用不匹配
'(分子::initSimilarity(int)::) (原子)'
DataStructures/Molecule.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;
}
以及从it:
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;
}
使用了 typedefs:
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;
}
This is the problematic function:
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
}
And the output:
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)::
I have no idea how this happens and what exactly that means, and I couldn't find any help on Google either...
The called function (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;
}
and the one called from it:
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;
}
used typedefs:
typedef dynamic_bitset<> AtomSet;
typedef dynamic_bitset<>::size_type atom_ind;
and of course, the function which stars in the error output:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到 lambda 参数类型中的引用,但
getSetMask
所需的函子类型中没有引用。如果您使这些保持一致,您的错误是否仍然存在?
即如果你想要修改
Atom
的操作,你需要I see references in the argument type of the lambda, and no references in the functor types needed by
getSetMask
.Does your error persist if you make those consistent?
i.e. if you want action to modify the
Atom
, you need