2011-12-19 48 views
2

編譯錯誤:的remove_if()編譯錯誤,在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 compiled 
with 
[ 
    _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 compiled 
with 
[ 
    _FwdIt=std::_Tree_const_iterator,std::allocator,true>>>, 
    _Mytree=std::_Tree_val,std::allocator,true>>, 
    _Pr=triangleIsCompleted 
] 

我認爲這個問題是在傳遞參數給STL的remove_if(),由編譯器錯誤的建議。我已經添加了以下注釋行:

//**** 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 
    // ... 
} 
+2

什麼是'triangleSet'?也許它是'typedef std :: set '?看來'triangleSet :: begin()'和'triangleSet :: end()'方法返回了'const'迭代器,因此你不能修改這個集合。 – 2011-12-19 20:06:06

+0

typedef multiset triangleSet; //三角形集合incase的類型def給出了任何線索 – puneetk 2011-12-20 06:33:09

回答

5

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; 
     } 
    } 
}