Enzo
Enzo

Reputation: 962

Using QPoint with std::map in C++

I have QPoint variables in a vector and i want to store it in a map.

std::map<QPoint, int> pointMap;
QPoint t;
int i;
pointMap.insert(std::pair<QPoint,int>(t, i));

If i use string,int map, it works fine. However i can't use QPoint in a map. Any ideas?

Compiler message: "no match for call to (std::pair)(QPoint&, int)"

Upvotes: 2

Views: 2378

Answers (3)

George
George

Reputation: 630

Just wanted to point out that using inline keyword might be a good idea since it helps to avoid multiple definition error:

inline bool operator<(const QPointF &p1, const QPointF &p2);

Upvotes: 0

Rakib
Rakib

Reputation: 7625

QPoint does not have operator< overloaded, which is required while inserting in a map to compare with other entries and find the right position. That's why the insertion will not work. you can define your own comparison method and use it while creating the map like

     std::map<T1, T2, less ..>

Upvotes: 0

vahancho
vahancho

Reputation: 21250

I think the problem is that std::map requires the type that has operator < implemented, however QPoint does not. To resolve this, you could define your map, for example, in the following way:

std::map<int, QPoint> pointMap;

Otherwice you need to define your custom "Less than" operator for QPoint, like:

bool operator <(QPoint point1, QPoint point2)
{
    // Do you logic here, to compare two points.
    return true;
}

Upvotes: 3

Related Questions