Question

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)"

Was it helpful?

Solution

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

OTHER TIPS

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 ..>

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top