+ 2

map operator== is not working for custom data types ?

After creating a map of char(key) and NewClass(value). And creating a copy of the same map, Comparing these two maps dosen't work. Whereas, instead of using NewClass as value if I use an int, It works well. The code is placed below. https://code.sololearn.com/cWu3ZoiW2ZkX/?ref=app

6th Mar 2020, 6:20 PM
Terminal_Phantom
Terminal_Phantom - avatar
3 Respostas
+ 2
map can't use your equality operator because it compares const objects. You equality operator should be defined with const qualifier: line #8 bool operator==(const NewClass& ref){ should be fixed like this bool operator==(const NewClass& ref) const{
6th Mar 2020, 9:00 PM
andriy kan
andriy kan - avatar
+ 2
Thanks Shadow and andrly kan. Now it's working fine
7th Mar 2020, 4:20 AM
Terminal_Phantom
Terminal_Phantom - avatar
+ 1
Try defining the equality operator as a non-member function outside the struct, i.e. bool operator == ( const NewClass& left, const NewClass& right ) { if ( left.value == right.value ) return true; return false; }
6th Mar 2020, 6:35 PM
Shadow
Shadow - avatar