2012-03-17 58 views
0

我正在開發一個在Ubuntu 11.04上使用NetBeans 6.9的C++應用程序。我正在使用OpenCV 2.3.1。我想知道是否有人能告訴我這段代碼有什麼問題。OpenCV,C++:插入集

void AddTriangle(CvPoint buf[3], set< Triangle > &V)
{
Triangle triangle;
int inc;

for (inc=0; inc<3; ++inc) 
{ 
    triangle.v[inc].x=buf[inc].x; 
    triangle.v[inc].y=buf[inc].y; 
} 
V.insert((const Triangle) triangle); 

}

我收到以下錯誤消息當我嘗試編譯。

from /usr/include/c++/4.5/bits/locale_classes.h:42,
from /usr/include/c++/4.5/bits/ios_base.h:43,
from /usr/include/c++/4.5/ios:43,
from /usr/include/c++/4.5/istream:40,
from /usr/include/c++/4.5/sstream:39,
from /usr/include/c++/4.5/complex:47,
from /usr/local/include/opencv2/core/core.hpp:59,
from ../../OpenCV-2.3.1/include/opencv/cv.h:64,
from ../../OpenCV-2.3.1/include/opencv/cv.hpp:50,
from ../../DraculaFiles/TwoDTriangulation.cpp:12:
/usr/include/c++/4.5/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = sTriangle]’:
In file included from /usr/include/c++/4.5/string:50:0, /usr/include/c++/4.5/bits/stl_tree.h:1184:4: instantiated from ‘std::pair, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = sTriangle, _Val = sTriangle, _KeyOfValue = std::_Identity, _Compare = std::less, _Alloc = std::allocator]’
/usr/include/c++/4.5/bits/stl_set.h:408:29: instantiated from ‘std::pair, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = sTriangle, _Compare = std::less, _Alloc = std::allocator, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator, value_type = sTriangle]’

三角定義如下。

typedef struct sTriangle
{
CvPoint v[3];
} Triangle;

任何幫助將不勝感激,
彼得。

+2

我有一種感覺,就是不完整的錯誤輸出。應該有一行中包含「錯誤」一詞。 – Irfy 2012-03-17 19:39:46

+0

當您檢索錯誤輸出的其餘部分時,是否有理由不在本地範圍內使用for(int inc = 0; ...)來定義循環計數器? – thb 2012-03-17 19:47:31

+0

其餘錯誤消息是 – OtagoHarbour 2012-03-17 22:05:57

回答

1

要將Triangle放入一個集合中,您必須實現比較運算符。否則,該設置無法分辨哪些值相等,哪些不相同。

typedef struct sTriangle 
{ 
CvPoint v[3]; 
bool operator<(const sTriangle& other) const; 
} Triangle; 

bool sTriangle::operator<(const sTriangle& other) const 
{ 
    // compare 'this' with 'other' 
} 

對於實現較少的運算符,可以激發here

編輯:您可以實現更少的操作,即使沒有修改sTriangle結構:

typedef struct sCvPoint { 
int x; 
int y; 
} CvPoint; 

typedef struct sTriangle 
{ 
CvPoint v[3]; 
} Triangle; 

bool operator==(CvPoint const& left, CvPoint const& right) 
{ 
    return left.x == right.x && left.y == right.y; 
} 

bool operator<(CvPoint const& left, CvPoint const& right) 
{ 
    return left.x == right.x 
     ? left.y < right.y 
     : left.x < right.x; 
} 

bool operator<(Triangle const& left, Triangle const& right) 
{ 
    return left.v[0] == right.v[0] 
     ? left.v[1] == right.v[1] 
      ? left.v[2] < right.v[2] 
      : left.v[1] < right.v[1] 
     : left.v[0] < right.v[0]; 
} 
1

STL無法區分集合中三角形的實例,並且它正在查找超載的「<」運算符。