gpt4 book ai didi

c++ - remove_if() 编译错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:44:30 24 4
gpt4 key购买 nike

VS2010编译错误:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1840): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const triangle' (or there is no acceptable conversion)h:\kingston_backup\ocv\ocv\delaunay.h(281): could be 'triangle &triangle::operator =(const triangle &)'while trying to match the argument list '(const triangle, const triangle)'c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1853) : see reference to function template instantiation '_FwdIt std::_Remove_if,_Pr>(_FwdIt,_FwdIt,_Pr)' being compiledwith[  _FwdIt=std::_Tree_unchecked_const_iterator,std::allocator,true>>>,  _Mytree=std::_Tree_val,std::allocator,true>>,  _Pr=triangleIsCompleted]h:\kingston_backup\ocv\ocv\delaunay.cpp(272) : see reference to function template instantiation '_FwdIt std::remove_if,triangleIsCompleted>(_FwdIt,_FwdIt,_Pr)' being compiledwith[  _FwdIt=std::_Tree_const_iterator,std::allocator,true>>>,  _Mytree=std::_Tree_val,std::allocator,true>>,  _Pr=triangleIsCompleted]

I think the problem is in passing the arguments to the remove_if() of the STL, as suggested by the compiler error. I have added the following comment to the line:

//**** ERROR LINE

class triangleIsCompleted
{
public:
triangleIsCompleted(cvIterator itVertex, triangleSet& output, const vertex SuperTriangle[3])
: m_itVertex(itVertex)
, m_Output(output)
, m_pSuperTriangle(SuperTriangle)
{}

bool operator()(const triangle& tri) const
{
bool b = tri.IsLeftOf(m_itVertex);

if (b)
{
triangleHasVertex thv(m_pSuperTriangle);
if (! thv(tri)) m_Output.insert(tri);
}
return b;
}
};

// ...

triangleSet workset;
workset.insert(triangle(vSuper));

for (itVertex = vertices.begin(); itVertex != vertices.end(); itVertex++)
{
tIterator itEnd = remove_if(workset.begin(), workset.end(), triangleIsCompleted(itVertex, output, vSuper)); //**** ERROR LINE
// ...
}

最佳答案

remove_if 不会删除任何内容(在删除的意义上)。它复制周围的值,以便所有剩余值都在范围的开头结束(并且范围的其余部分或多或少处于未指定的状态)。

由于关联容器中的键是不可变的,因此不可能将值从一个位置复制到另一个位置,因此 remove_if 不能用于它。

标准库似乎不包含 set 的 remove_if,因此您必须自己动手。它可能看起来像这样:

#include <set>

template <class Key, class Compare, class Alloc, class Func>
void erase_if(std::set<Key, Compare, Alloc>& set, Func f)
{
for (typename std::set<Key, Compare, Alloc>::iterator it = set.begin(); it != set.end(); ) {
if (f(*it)) {
set.erase(it++); //increment before passing to erase, because after the call it would be invalidated
}
else {
++it;
}
}
}

关于c++ - remove_if() 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8566799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com