0

私は一般的なマップオブジェクトを持っています。operator[] をオーバーロードしたいのでmap[key]、キーの値を返します。添字演算子の 2 つのバージョンを作成しました。

非定数:

ValueType& operator[](KeyType key){

定数:

const ValueType& operator[]( KeyType&   key) const{

非 const バージョンは正常に動作しますが、const Map を作成すると問題が発生します。主に次のように書きます。

     const IntMap map5(17);
     map5[8];

そして、私はこれらのエラーを受け取ります:

ambiguous overload for 'operator[]' (operand types are 'const IntMap {aka const mtm::MtmMap<int, int>}' and 'int')  


invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'   
4

1 に答える 1