c++ - OpenCV,C++ : Inserting into set

标签 c++ list opencv netbeans-6.9 ubuntu-11.04

我正在 Ubuntu 11.04 上使用 NetBeans 6.9 开发 C++ 应用程序。我正在使用 OpenCV 2.3.1。我想知道是否有人可以告诉我这段代码有什么问题。

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

<pre><code>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); </code></pre>

}

我在尝试编译时收到以下错误消息。

from /usr/include/c++/4.5/bits/locale_classes.h:42,<br/> from /usr/include/c++/4.5/bits/ios_base.h:43,<br/> from /usr/include/c++/4.5/ios:43,<br/> from /usr/include/c++/4.5/istream:40,<br/> from /usr/include/c++/4.5/sstream:39,<br/> from /usr/include/c++/4.5/complex:47,<br/> from /usr/local/include/opencv2/core/core.hpp:59,<br/> from ../../OpenCV-2.3.1/include/opencv/cv.h:64,<br/> from ../../OpenCV-2.3.1/include/opencv/cv.hpp:50,<br/> from ../../DraculaFiles/TwoDTriangulation.cpp:12:<br/> /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]’:<br/> 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]’<br/> /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<br/> {<br/> CvPoint v[3];<br/> } Triangle;

如有任何帮助,我们将不胜感激,
彼得。

最佳答案

要将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'
}

对于less操作符的实现,可以启发here .

编辑:即使不修改 sTriangle 结构,您也可以实现 less 运算符:

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

关于c++ - OpenCV,C++ : Inserting into set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9753075/

相关文章:

python - 如何从计算中排除列表中的最大数字

c++ - 使用 Sublime Text 3 在 Powershell 中构建 C++ 而不是 CMD

c++ - 模板非类型参数

python - 检查元组是否包含多个值中的至少一个

list - 选择列表的最后一个元素

c++ - 在给定点拾取 Blob 的更快方法

c++ - 无法在函数内部为 cv::Mat 赋值

python - 从源代码安装opencv时,如何在pyinstaller中包含OpenCV?

c++ - 如何创建具有预定义状态的自定义 Quick QML 项

c++ - Qt:QAudioInput 与 QAudioRecorder