简体   繁体   中英

Adding a b2vec2 in a map with std::pair key and value

I have a error when I'm trying to add element to my map, here is my simplified code :

I have a map :

std::map<std::pair<std::string, std::pair<int, b2Vec2> >, std::pair<std::string, std::pair<int, b2Vec2> > > myMap; `  

b2Vec2 rVec (1, 1)  
std::pair<std::string, std::pair<int, b2Vec2> > partA (std::make_pair ("test", std::make_pair(1, rVec ) ) );  
std::pair<std::string, std::pair<int, b2Vec2> > partB (std::make_pair ("testb", std::make_pair(1, rVec ) ) );` 

myMap[partA] = partB; //When I add this line i get an error when compiling.`

I get the error : "Invalid operands to binary expression ('const b2Vec2' and 'const b2Vec2')." If I replace every b2Vec2 type by a int type in myMap definition and initialization, it works. So I think there is just an issue with b2Vec2 but I don't get it.

The type used as a key in a std::map needs to have a less than comparison operator defined for it ( operator< ). std::pair defines one of these, which requires that operator< be defined for the types in the pair.

In your case, you're getting the compile error because the b2Vec2 type does not define operator< .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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