I have a map which has a class as key.
In this class I overloaded the < operator, I read here that the map will automatically use that for comparison and sorting.
I get the following error when compiling:
Error 1 error C2664: 'std::pair::pair(const std::pair &)' : cannot convert argument 1 from 'Position' to 'Position *const &' e:\program files\visual studio 2013 ultimate\vc\include\xmemory0
I don't get what that really means or how I can avoid it. It doesn't highlight anything in my code either.
Here is my code, my class is coordinates, I want them to be sorted from left to right and top to bottom, but the logic isn't the problem here.
There's obviously more stuff in the Position files, but I think these are the relevant parts.
main.cpp
int main()
{
std::map<Position*, int> karte;
Position p1;
p1.setX(0);
p1.setY(0);
Position p2;
p2.setX(0);
p2.setY(1);
Position p3;
p3.setX(1);
p3.setY(0);
Position p4;
p4.setX(1);
p4.setY(1);
karte.emplace(p1, 1);
karte.emplace(p2, 2);
karte.emplace(p3, 3);
karte.emplace(p4, 4);
for (auto& x : karte)
{
std::cout << x.first->toString() << ": " << x.second << std::endl;
}
return 0;
}
Position.h
bool operator<(const Position&) const;
Position.cpp
bool Position::operator<(const Position &position) const
{
if ((x_ < position.x_) && (y_ == position.y_))
{
return true;
}
if (x_ > position.x_ && y_ < position.y_)
{
return true;
}
if (x_ == position.x_ && y_ < position.y_)
{
return true;
}
else
{
return false;
}
}
Aucun commentaire:
Enregistrer un commentaire