简体   繁体   English

创建std :: pair时出现“ operator &lt;”错误<A,B>

[英]'operator<' error while creating an std::pair<A,B>

I already have a workaround for this but would still like to understand the problem. 我已经有解决方法,但仍然想了解问题。

I have a Multimap 我有一个多图

multimap<QPoint,Figure*> mymap;

QPoint is a class from Qt 5.4. QPoint是Qt 5.4中的类。 Figure* is a pointer to a class I have concocted myself. Figure *是我自己设计的类的指针。

Now I want to add elements to that map. 现在,我想向该地图添加元素。

This sample works fine: 此示例工作正常:

multimap<int,int> test;
test.insert(pair<int,int>(41,43));

As does this one (being said workaround) 就像这个(正在说解决方法)一样

std::pair<QPoint,Figure*> p;
p.first = pos;
p.second =  sub_fig;
mymap.insert(p);

However, the plain first reflex of 但是,

std::pair<QPoint,Figure*> p(pos, sub_fig);

has the compiler at that line state something like: 使编译器处于该行状态,例如:

[..]
scanner.cpp:264:17:   required from here
/usr/include/c++/4.9/bits/stl_function.h:371:20: error: no match for
‘operator<’ (operand types are ‘const QPoint’ and ‘const QPoint’)
  { return __x < __y; }
[..]

followed by the usual five kilometers of stacked STL error messages. 然后是通常的五公里堆积的STL错误消息。 First: The 'types' are not QPoint and QPoint. 第一:“类型” 不是 QPoint和QPoint。 They are, as stated above, QPoint and Figure*. 如上所述,它们是QPoint和Figure *。

Anyone who can riddle this? 任何人都可以对此困惑吗?

CORRECTION 更正

My work-around does not work either after all. 毕竟,我的解决方法也不起作用。 I had forgotten to de-comment res.insert(p); 我忘记注释res.insert(p);

Here is the complete pertinent code: 这是完整的相关代码:

multimap<QPoint,Figure*> res;
// ...
vector<Figure*> stack = figure->get_above_figure_stack();
  for (vector<Figure*>::const_iterator CI2=stack.begin();
    CI2!=stack.end();CI2++)
  {
    // ..
    Figure* sub_fig = *CI2;
    std::pair<QPoint,Figure*> p;
    p.first = pos;
    p.second =  sub_fig;
    res.insert(p); // <- The bad line.
  }

The keys in a multimap are ordered by default with std::less which invokes operator< on the key type. 默认情况下,多multimap中的键使用std::less排序,该键在键类型上调用operator<

Your key object (QPoint) has no operator< to do the comparison. 您的关键对象(QPoint)没有operator<进行比较。

You will need to provide your own comparison function using the approprate multimap constructor. 您将需要使用适当的多图构造函数提供自己的比较功能。

multimap needs an ordering relation for the keys, and its default is to use < (in the guise of std::less ). multimap需要按键的排序关系,其默认值是使用< (以std::less为幌子)。

Since QPoint doesn't have an overload of operator< , the compiler is complaining that it doesn't exist. 由于QPoint没有operator<的重载,因此编译器抱怨它不存在。

It's not difficult to provide one: 提供以下内容并不难:

bool operator< (const QPoint& lhs, const QPoint& rhs)
{
    return lhs.x() < rhs.x() || (lhs.x() == rhs.x() && lhs.y() < rhs.y());
}

or 要么

bool lessQPoints (const QPoint& lhs, const QPoint& rhs)
{
    return lhs.x() < rhs.x() || (lhs.x() == rhs.x() && lhs.y() < rhs.y());
}

multimap<QPoint, Figure*, lessQPoints> mymap;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM