Using map with class error, compile error

后端 未结 3 1925
自闭症患者
自闭症患者 2021-01-28 02:49

I have the following compiler error, how could I fix it?

error:  instantiated from `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&a         


        
3条回答
  •  春和景丽
    2021-01-28 03:30

    You need a comparison function for the map. You can either create operator< that compares two instances of ar, or you can create a custom function and pass it as the 3rd template parameter.

    An example of the former might be:

    class ar {
      ...
      bool operator<(const ar& rhs) const {
        return std::tie(a,b,c) < std::tie(rhs.a, rhs.b, rhs.c);
      }
      ...
    };
    

提交回复
热议问题